0

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
3
  • You should use recursion for this. Commented Jun 21, 2013 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. Commented Jun 21, 2013 at 19:53
  • Thanks for the comments, but why was I dinged for this? Commented Jun 21, 2013 at 20:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks James! I'll give this a try today.

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.