Skip to content

Instantly share code, notes, and snippets.

@LarrySul
Last active September 9, 2021 08:27
Show Gist options
  • Save LarrySul/95a52f030fafe299e3737f345130a375 to your computer and use it in GitHub Desktop.
Save LarrySul/95a52f030fafe299e3737f345130a375 to your computer and use it in GitHub Desktop.
find the maximum value in a series of nested arrays
<?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