note 115211 added to function.max
| From: | ReVo_ | Date: | Thu, 12 Jun 2014 15:28:54 +0000 |
| Subject: | note 115211 added to function.max | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199674@lists.php.net to get a copy of this message | ||
Sometimes you could need to get the max from an array which looks like this:
<?php
$arrTest = array(
array( "day" => 1, "b" => 10 ),
array( "day" => 2, "b" => 43 ),
array( "day" => 3, "b" => 2 ),
array( "day" => 4, "b" => -3 ),
array( "day" => 5, "b" => 4 ),
array( "day" => 6, "b" => -5 )
);
?>
all max functions i see around don't provide a way to get the max value of the values in
(example) key "b"
<?php
function maxValueInArray($array, $keyToSearch)
{
$currentMax = NULL;
foreach($array as $arr)
{
foreach($arr as $key => $value)
{
if ($key == $keyToSearch && ($value >= $currentMax))
{
$currentMax = $value;
}
}
}
return $currentMax;
}
// array key
$value = maxValueInArray($arrTest, "b");
?>
output: 43
----
Server IP: 81.88.49.41
Probable Submitter: 79.36.16.126
----
Manual Page -- http://php.net/manual/en/function.max.php
Edit -- https://master.php.net/note/edit/115211
Del: integrated -- https://master.php.net/note/delete/115211/integrated
Del: useless -- https://master.php.net/note/delete/115211/useless
Del: bad code -- https://master.php.net/note/delete/115211/bad+code
Del: spam -- https://master.php.net/note/delete/115211/spam
Del: non-english -- https://master.php.net/note/delete/115211/non-english
Del: in docs -- https://master.php.net/note/delete/115211/in+docs
Del: other reasons-- https://master.php.net/note/delete/115211
Reject -- https://master.php.net/note/reject/115211
Search -- https://master.php.net/manage/user-notes.php