My requirement is to find the largest / maximum value in the array, which may contain other arrays inside it. For example, we could take a look at the array below.
$array = array( 13, array(10, 4, 111, 3), 4, array(23, 450, 12,array(110, 119, 20, 670), 45 ,45,67,89), ); $max = find_max($array, 0); print "Maximumum Value is $max";
I already have a find_max working function, but all I wanted to know was that there might be a better and more efficient way to do it differently than the code below.
function find_max($array, $maxValue) { foreach ($array as $member) { if (is_array($member)) { $maxValue = find_max($member, $maxValue); } else { if($member==$maxValue){ continue; } if ($member > $maxValue) { $maxValue = $member; } } } return $maxValue; }
source share