Last active
September 9, 2021 08:27
-
-
Save LarrySul/95a52f030fafe299e3737f345130a375 to your computer and use it in GitHub Desktop.
find the maximum value in a series of nested arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function findMaxValues (array $values) :int | |
{ | |
if(!is_array($values)) return "not an array"; | |
$max_val = 0; | |
foreach($values as $item){ | |
if(is_array($item)){ | |
$check_highest_val = findMaxValues($item); | |
}else{ | |
$check_highest_val = $item; | |
} | |
$max_val = $check_highest_val > $max_val ? $check_highest_val : $max_val; | |
} | |
return $max_val; | |
} | |
print_r(findMaxValues([1, [2, 3, [8, 19]]])); // 19 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment