note 58565 added to function.max
| From: | (NOSPAM) | Date: | Tue, 08 Nov 2005 09:56:26 +0000 |
| Subject: | note 58565 added to function.max | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98244@lists.php.net to get a copy of this message | ||
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!
----
Manual Page -- http://www.php.net/manual/en/function.max.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58565
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58565&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58565&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58565&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58565&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58565&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58565&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58565&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58565&report=yes
Search -- http://master.php.net/manage/user-notes.php