Is there a way to iterate over a multidimensional array without knowing its depth?

So far, if I need to iterate over a multidimensional array, I use a foreach loop for each dimension.

for example, for two measurements

foreach($array as $key=>$value) { foreach($value as $k2=>$v2) { echo } } 

What should I do when I do not know the depth of the array? those. depth is variable.

The only thing I can think of is to encode the whole loop of loops and break the loop if the next value is not an array. It seems a little silly.

Is there a better way?

+9
arrays php nested-loops
source share
4 answers

Yes, you can use recursion . Here is an example of the output of all array elements:

 function printAll($a) { if (!is_array($a)) { echo $a, ' '; return; } foreach($a as $v) { printAll($v); } } $array = array('hello', array('world', '!', array('whats'), 'up'), array('?')); printAll($array); 

What you should always remember when doing a recursion is that you need a base case where you will no longer go deep.

I like to check the base case before continuing the function. This is a common idiom, but it is not necessary. You can also check the foreach just as well if you need to output or make a recursive call, but I often find it harder to maintain this path for this.

The "distance" between your current input and the base register is called an option and is an integer. Variant must be strictly reduced in all recursive calls. A variation in the previous example is the depth of $a . If you do not think about this option, you run the risk of getting endless recursions, and in the end the script will die because of. It is not uncommon to accurately document that this option is in a comment before recursive functions.

+18
source share

You can use recursion for this problem:

Here is one example.

 $array = array(1 => array(1 => "a", 2 => array(1 => "b", 2 => "c", 3 => array(1 => "final value")))); //print_r($array); printAllValues($array); function printAllValues($arr) { if(!is_array($arr)) { echo '<br />' . $arr; return; } foreach($arr as $k => $v) { printAllValues($v); } } 

It will use recursion to loop through the array

It will print as

 a b c final value 
+1
source share

You can execute the function below for looping through a multidimensional array without knowing its depth.

 // recursive function loop through the dimensional array function loop($array){ //loop each row of array foreach($array as $key => $value) { //if the value is array, it will do the recursive if(is_array($value) ) $array[$key] = loop($array[$key]); if(!is_array($value)) { // you can do your algorithm here // example: $array[$key] = (string) $value; // cast value to string data type } } return $array; } 

using the above function, it will go through each multidimensional array, the following is an example of an array that you can pass to the loop function:

  //array sample to pass to loop() function $data = [ 'invoice' => [ 'bill_information' => [ 'price' => 200.00, 'quantity' => 5 ], 'price_per_quantity' => 50.00 ], 'user_id' => 20 ]; // then you can pass it like this : $result = loop($data); var_dump($result); //it will convert all the value to string for this example purpose 
0
source share

A simple function inside array_walk_recursive to display the level of nesting and keys and values:

 array_walk_recursive($array, function($v, $k) { static $l = 0; echo "Level " . $l++ . ": $k => $v\n"; }); 

Another showing use with a link to get the result:

 array_walk_recursive($array, function($v) use(&$result) { $result[] = $v; }); 
-one
source share

All Articles