Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a read only multidimensional associative array I need to extract specific values from to generate output with some of the values.

The array looks like this:

array (
'Dogs' => 
array (
0 => 
array (
  'Name' => 'Big Dogs',
  'ID' => '32',
  'Brown dogs' => 
  array (
    0 => 
    array (
      'Name' => '4 legged dogs',
      'Retrievers' => 
      array (
        0 => 
        array (
          'Name' => 'Fido',
          'ID' => '11',
          'Owner' => 'Billy',
          'IsaBiter' => true,
        ),
      ),
    ),
    1 => 
    array (
       'Name' => '3 legged dogs',
       'Retrievers' => 
        array (
        0 => 
        array (
           'Name' => 'Spot',
          'ID' => '12',
          'Owner' => 'Sally',
          'IsaBiter' => false,
        ),
      ),
    ),

etc..

And a nested foreach loop that runs through the array that can print all values of the array as follows:

echo "<ul>";
 foreach($myArray as $arr1 => $val1) { 
  foreach($val1 as $arr2 => $val2) {
    foreach($val2 as $val3) {
     echo "<li>" . $val3 . "<ul>";
         foreach($val3 as $arr4 => $val4) {
         foreach($val4 as $arr5 => $val5) {
         echo "<li>" . $val5;
        foreach($val5 as $arr6 => $val6) {
        //echo $val6;
                foreach($val6 as $arr7 => $val7) {
            echo $val7 . "<br />"; //dumps the details
                    }
                    echo "</li>";
               }
                 }             
        }
        echo "</ul>";
        echo "</li>";
       }
   }
}

Ideally I would like to be able to 1) exclude values I do not want show (ID, certain array level names etc, currently it shows all) and 2) display specific ones (ie Name, owner, IsaBiter) so I can format the results cleaner, similar to this:

Big Dogs
   Brown Dogs
     Retrievers
       Name: Fido
       Owner: Billy
       IsaBiter: true

       Name: Spot
       Owner: Sally
       IsaBiter: false
share|improve this question
 
You should use recursion for this. –  Wayne Whitty Jun 21 at 19:51
 
When you use foreach ($array AS $key => $value) {}, you could compare $key with what you want to show (it's just a string). Anyway, filtering a level will be more complicated, you should include an iterator value (an integer counting levels) and exclude when properly. Maybe, As @WayneWhitty said, using recursion would be better. –  Alejandro Iván Jun 21 at 19:53
 
Thanks for the comments, but why was I dinged for this? –  bridgemanusa Jun 21 at 20:47

1 Answer

Here's the start of a recursive function that comes close to matching what you want.

function displayArray($array, $level = 0) {
  // This filter determines which levels are displayed
  $visible = in_array($level, array(0,2,4,6));

  if ($visible) {
    echo '<ul>';
    foreach($array as $key => $value) { 
      if (is_array($value)) {
        echo '<li>';
        echo $key;
        displayArray($value, $level + 1);
        echo '</li>';
      }                    
      else {
        // This filter determines which non-array keys are displayed
        $visible = $level == 6 and in_array($key, array('Name','Owner','IsaBiter'));

        if ($visible) {
          if (is_bool($value)) $value = $value ? "true" : "false";
          echo '<li>';
          echo "$key : $value"; 
          echo '</li>';
        }  
      }
    }
    echo '</ul>';
  }  
  else {
    // If a level is not visible, we still try and display child arrays

    foreach($array as $key => $value)
      if (is_array($value))
        displayArray($value, $level + 1);
  }                           
}

I've shown how you can filter out specific levels, and specific keys, but you'll probably need to do more than that to get the exact output you've shown.

First, in your example you've only shown "Retrievers" once, although "Fido" and "Spot" are actually from two separate "Retrievers" arrays (4 legged retrievers and 3 legged retrievers). I'm not sure if that is intentional, and if so, how exactly that rule should work.

Second, you have "Big Dogs" displayed as if it were the key of the top level array, when in fact it's the "Name" value of one of the children of the items in that array. Again, if that is intentional, I'm not exactly sure what the rule if for deciding what you want displayed and how.

If this answer is not good enough for you to figure out the rest yourself, you will need to provide more sample data and sample output so I can determine exactly what you are trying to achieve.

share|improve this answer
 
Thanks James! I'll give this a try today. –  bridgemanusa Jun 24 at 13:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.