Skip to content

Instantly share code, notes, and snippets.

@tchalvak
Created April 8, 2015 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tchalvak/7b0567f9e854e6d22205 to your computer and use it in GitHub Desktop.
Save tchalvak/7b0567f9e854e6d22205 to your computer and use it in GitHub Desktop.
recursive_integer_maximum
<?php
// Recursively determine the max
// Does not handle badly formed input
// Not yet optimized for performance
function my_max( $data ) {
$max = null;
foreach($data as $elem){
$new_max = is_array($elem)? my_max($elem) : $elem;
$max = $new_max > $max? $new_max : $max;
}
return $max;
}
echo my_max( array( 1, 2, 3, 4, 5 ) ); // 5
echo my_max( array( 1, 2, array( 5, 6 ), 3, 4 )); // 6
echo my_max( array( 1, 2, array( 5, 6, 7, 89, 213, -4), 3, 4, 55, 87, 44 )); // 213
echo my_max( array( -1, -2, array( -5, -6, -7, -89, -213, -4), -3, -4, -55, -87, -44, 0 )); // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment