note 58565 deleted from function.max by danbrown

From: Date: Tue, 22 Mar 2011 14:01:32 +0000
Subject: note 58565 deleted from function.max by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-178092@lists.php.net to get a copy of this message
Note Submitter: tim at (NOSPAM) dot crazynot2 dot com ---- In response to the previous two posters (zher0 at netcarrier dot com & walkingmantis): I was trying to do exactly what zher0 suggested; calculate the max value of a multi-dimensional array with variably sized 'sub-arrays'. Here is a simple little function I came up with to do just that: <?php function multimax( $array ) { // use foreach to iterate over our input array. foreach( $array as $value ) { // check if $value is an array... if( is_array($value) ) { // ... $value is an array so recursively pass it into multimax() to // determine it's highest value. $subvalue = multimax($value); // if the returned $subvalue is greater than our current highest value, // set it as our $return value. if( $subvalue > $return ) { $return = $subvalue; } } elseif($value > $return) { // ... $value is not an array so set the return variable if it's greater // than our highest value so far. $return = $value; } } // return (what should be) the highest value from any dimension. return $return; } ?> Please note that I have only performed very limited testing on this code -- be sure to check it thoroughly if you implement it somewhere!

« previous php.notes (#178092) next »