3

As seen here and some other places for each is well explained, but how do I use it with a "deeper" array?

I have this array:

Array ( 
[3] => Array ( [ort] => Array ( [0] => Brunnsdalen [1] => Agerum ) [kommun] => Array ( [0] => Alvesta kommun [1] => Arjeplogs kommun ) ) 
[4] => Array ( [ort] => Array ( [2] => Björstorp ) ) 
[6] => Array ( [kommun] => Array ( [2] => Arboga kommun ) ) ) 

And the closest I've gotten is this:

        foreach ($allaMedlemmarsIntel as $row => $innerArray){
                foreach($innerArray as $innerRow => $deeperInner){
                      foreach($deeperInner as $deeperinnerRow => $value){
                            echo $value . "<br/>";
                      }
                }

          }

How can I separate the output values?

I'd like to be able to display each value under [ort] as 'value' and all values under [kommun] as value – Alisso 1 min ago edit

Community
  • 1
  • 1
Alisso
  • 1,861
  • 1
  • 17
  • 32
  • hmm, I just might have found a good example here: http://stackoverflow.com/questions/10131802/foreach-for-arrays-inside-of-an-array?rq=1 (trying it out now) – Alisso Jun 24 '12 at 21:32
  • Nope.. couldn't get that to work :/ – Alisso Jun 24 '12 at 21:34
  • i don't get what you want to do ? if you want to display the array "nicely" you could just try this: `echo "
    ";print_r($allaMedlemmarsIntel);echo "
    ";`
    – HamZa Jun 24 '12 at 21:46

2 Answers2

4

This recursively outputs only the values but is easily adjusted for other output formats.

    function RecurseArray( $inarray ) 
   { 
       foreach ( $inarray as $inkey => $inval ) 
       { 
           if ( is_array( $inval ) ) 
           { 
               $toarray = RecurseArray( $inval ); 
           } 
           else 
           { 
               echo $inval."\n";  //handle non array
           } 
       } 
   } 
matt3141
  • 4,303
  • 1
  • 19
  • 24
-1

I got it!

This is my now working solution!

 foreach ($allaMedlemmarsIntel as $eachMedlemID => $varjeMedlemsIntel)
              {
                    echo '<div class="each">';
                          echo $eachMedlemID;
                          echo '<ul class="omradenOchPlatser">';
                          foreach($varjeMedlemsIntel as $typAvPlats => $deeperInner){
                                      foreach($deeperInner as $deeperinnerRow => $value){
                                            echo '<li class="'.$typAvPlats.'">';
                                            echo $value;
                                            echo '</li>';
                                      }
                          }
                          echo '</ul>';
                    echo '</div>';
              }
Alisso
  • 1,861
  • 1
  • 17
  • 32