Created
April 8, 2015 15:00
-
-
Save tchalvak/7b0567f9e854e6d22205 to your computer and use it in GitHub Desktop.
recursive_integer_maximum
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 | |
// 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