megaman
megaman

Reputation: 1101

php foreach on array when arrays might be nested

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)

Upvotes: 2

Views: 330

Answers (5)

Parag Tyagi
Parag Tyagi

Reputation: 8970

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");

Upvotes: 0

Faris
Faris

Reputation: 917

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.

Upvotes: 0

iamsleepy
iamsleepy

Reputation: 540

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.

Upvotes: 1

Victory
Victory

Reputation: 5890

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);

Upvotes: 0

Callidior
Callidior

Reputation: 2904

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).

Upvotes: 0

Related Questions