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

3
  • hmm, I just might have found a good example here: stackoverflow.com/questions/10131802/… (trying it out now) Commented Jun 24, 2012 at 21:32
  • Nope.. couldn't get that to work :/ Commented Jun 24, 2012 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 "<pre>";print_r($allaMedlemmarsIntel);echo "</pre>"; Commented Jun 24, 2012 at 21:46

2 Answers 2

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
           } 
       } 
   } 
Sign up to request clarification or add additional context in comments.

1 Comment

Where does that display anything?
-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>';
              }

1 Comment

Ids inte skriva kod på annat språk än engelska :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.