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)

asked Apr 5, 2014 at 17:46
3
  • 2
    Recursion is what you look for Commented Apr 5, 2014 at 17:48
  • 1
    Do you care about ever closing those <p> tags you open? Commented Apr 5, 2014 at 17:52
  • This is not a HTML question, i only use thoes to make linebreaks in the output oh and yes i do know what <br /> and print_r() do. Commented Apr 5, 2014 at 18:10

5 Answers 5

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.

answered Apr 5, 2014 at 17:56
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).

answered Apr 5, 2014 at 17:51
1
  • 1
    True except the "This is the way, print_r does it." part, which is not quite true. print_r does it alot better. Commented Apr 5, 2014 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);
answered Apr 5, 2014 at 17:55
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.

answered Apr 5, 2014 at 18:04
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");
answered Apr 5, 2014 at 17:59
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.