2

The following code uses foreach on an array and if the value is an array it does a for each on the nested array

foreach ($playfull as $a)
{
    if (is_array($a))
    {
        foreach ($a as $b)
        {
            print($b);
            print("<p>");
        }
    } else {
        print($a);
        print("<p>");
    }
}

This only works if you know that the arrays may only be nested one level deep

If arrays could be nested an unknown number of levels deep how do you achieve the same result? (The desired result being to print the value of every key in every array no matter how deeply nested they are)

megaman
  • 1,035
  • 1
  • 14
  • 21

5 Answers5

1

You can use array_walk_recursive. Example:

array_walk_recursive($array, function (&$val) 
{ 
      print($val);
    }

This function is a PHP built in function and it is short.

iamsleepy
  • 550
  • 3
  • 7
0

Use recursive functions (that are functions calling themselves):

function print_array_recursively($a)
{
    foreach ($a as $el)
    {
        if (is_array($el))
        {
            print_array_recursively($el);
        }
        else
        {
            print($el);
        }
    }
}

This is the way, print_r could do it (see comments).

Callidior
  • 2,899
  • 2
  • 18
  • 28
  • 1
    True except the "This is the way, print_r does it." part, which is not quite true. `print_r` does it alot better. – MightyPork Apr 05 '14 at 17:53
0

You want to use recursion, you want to call your printing function in itself, whenever you find an array, click here to see an example

$myArray = array(
   "foo",
   "bar",
   "children" => array(
     "biz",
     "baz"),
   "grandchildren" => array(
     "bang" => array(
       "pow",
       "wow")));


 function print_array($playfull) 
 {
   foreach ($playfull as $a)
     {
       if (is_array($a))
         {
           print_array($a);
         } else {
           echo $a;
           echo "<p>";
       }
     }
 }

 echo "Print Array\n";
 print_array($myArray);
Victory
  • 5,811
  • 2
  • 26
  • 45
0

Try this -

function array_iterate($arr, $level=0, $maxLevel=0) 
{ 
    if (is_array($arr)) 
    { 
        // unnecessary for this conditional to enclose 
        // the foreach loop 
        if ($maxLevel < ++$level) 
        { $maxLevel = $level; } 

        foreach($arr AS $k => $v) 
        { 
            // for this to work, the result must be stored 
            // back into $maxLevel 
            // FOR TESTING ONLY: 
            echo("<br>|k=$k|v=$v|level=$level|maxLevel=$maxLevel|"); 
            $maxLevel= array_iterate($v, $level, $maxLevel); 
        } 
        $level--; 
    } 

    // the conditional that was here caused all kinds 
    // of problems. so i got rid of it 
    return($maxLevel); 
} 

$array[] = 'hi'; 
$array[] = 'there'; 
$array[] = 'how'; 
$array['blobone'][] = 'how'; 
$array['blobone'][] = 'are'; 
$array['blobone'][] = 'you'; 
$array[] = 'this'; 
$array['this'][] = 'is'; 
$array['this']['is'][] = 'five'; 
$array['this']['is']['five'][] = 'levels'; 
$array['this']['is']['five']['levels'] = 'deep'; 
$array[] = 'the'; 
$array[] = 'here'; 

$var = array_iterate($array); 
echo("<br><br><pre>$var");
Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
0

You could use a recursive function, but the max depth will be determined by the maximum nesting limit (see this SO question, Increasing nesting functions calls limit, for details about increasing that if you need it)

Here's an example:

$array = array(1,array(2,3,array(4,5)),6,7,8);
function printArray($item)
{
    foreach ($item as $a)
    {
        if (is_array($a))
        {
            printArray($a);
        } else {
            print($a);
            print("<p>");
        }
    }
}
printArray($array);

I hope that helps.

Community
  • 1
  • 1
Faris
  • 877
  • 7
  • 9