Recursion means that the function calls itself to perform a process. For example, take a look at the following array:
Array
(
[0] => 1
[1] => 3
[2] => 6
[3] => Array
(
[0] => 8
[1] => 10
[2] => Array
(
[0] => 30
)
)
)
If we need to know the maximun integer into the elements of an array with multiple nested arrays, we can do it by using a recursive function... like this:
$test = array(
0 => 2,
1 => 50,
2 => array(
0 => 20,
3 => 10,
4 => 40,
)
);
function maxInteger($arr) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
maxInteger($value);
}
if (is_numeric($value) && $value > $max) {
$max = $value;
}
}
return $max;
}
print maxInteger($test);
That's all folks!!