Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Last active December 27, 2015 11:19
Show Gist options
  • Select an option

  • Save fhferreira/7317998 to your computer and use it in GitHub Desktop.

Select an option

Save fhferreira/7317998 to your computer and use it in GitHub Desktop.
Max Value Recursive in Array PHP
<?php
function MaxArray($arr){
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
// initialize $max
$iterator->next();
$max = $iterator->current();
// "iterate" over all values
foreach($iterator as $v) {
if ( $v > $max ) {
$max = $v;
}
}
return $max;
}
$arr = array(array(141,151,161), 2, 3, array(101, 202, array(303,404)));
echo MaxArray($arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment