This question already has an answer here:
I'm trying to guess how to iterate over multi-dimensional arrays with unknown number of dimensions. I've always done that job statically setting foreach
statements inside foreach
statements but I don't know how many statements to set this time because dimensions number is variable. In summary, All I need is to iterate over every element in a parent array that has more arrays as values and those values that are arrays have more arrays as values, and so on...
</div>
Recursion is all that you need:
function recur($arr) {
if (!is_array($arr)) {
// $arr is the last element
echo "$arr ";
return;
}
foreach($arr as $ar) recur($ar);
}