SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a way to make this shorter? MutliDimensional Array

    Im just echoing the results to see what it is, Im not really going to do that though.

    Multidimensional
    PHP Code:
    for ($i 0$i 9$i++)
    {
        if (!empty(
    $items[$i]['str'])) {
            echo 
    'str ' $items[$i]['str'] . '<br />';
            
    $str += $items[$i]['str'];
        }
        if (!empty(
    $items[$i]['dex'])) {
            echo 
    'dex ' $items[$i]['dex'] . '<br />';
            
    $dex += $items[$i]['dex'];
        }
        if (!empty(
    $items[$i]['str'])) {
            echo 
    'hp ' $items[$i]['hp'] . '<br />';
            
    $hp += $items[$i]['hp'];
        }
        if (!empty(
    $items[$i]['str'])) {
            echo 
    'luck ' $items[$i]['luck'] . '<br />';
            
    $luck += $items[$i]['luck'];
        }
        if (!empty(
    $items[$i]['ar'])) {
            echo 
    'ar ' $items[$i]['ar'] . '<br />';
            
    $ar += $items[$i]['ar'];
        }    
        
        if (!empty(
    $items[$i]['enc'])) {
            echo 
    'enc ' $items[$i]['enc'] . '<br />';
            
    $enc += $items[$i]['enc'];
        }    
        echo 
    '<br /><br />';

    I was thinking maybe something like this?
    PHP Code:
    $array = ('str''dex''hp''luck''ar''enc');
    $mods count($array);

    for (
    $i 0$i 9$i++)
    {
      for (
    $m 0$m $mods$m++)
      {
           if (!empty(
    $items[$i][$array[$m]]))
           {
                
    $array[$m] += $items[$i][$array[$m]];
           }
      }



  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Location
    Brisbane, Australia
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for ($i 0$i 9$i++) {
        foreach (array(
    'str''dex''hp''luck''ar''enc') as $key) {
            if (!empty(
    $items[$i][$key])) {
                
    printf('%s %s<br />'$key$items[$i][$key]);
                $
    $key += $items[$i][$key];
            } 
        }
        echo 
    '<br /><br />';


  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow that is awesome! Thanks Dude


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •