note 126214 added to function.max
| From: | chrisatcandmdtorgdtuk at main dot php dot net | Date: | Sat, 03 Jul 2021 15:36:16 +0000 |
| Subject: | note 126214 added to function.max | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216573@lists.php.net to get a copy of this message | ||
A function to return the key of the max value of an array. For multiple max values, it will return
the key of first. Change > to >= to get the last.
function array_max_key($arr) {
$maxkey='';
$maxval=false;
array_walk($arr,
function (&$cval,&$ckey) use(&$maxkey,&$maxval) {
if ($maxval === false || $cval > $maxval) {
$maxval = $cval;
$maxkey = $ckey;
}
}
);
return $maxkey;
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 180.249.42.31)
----
Manual Page -- https://php.net/manual/en/function.max.php
Edit -- https://main.php.net/note/edit/126214
Del: integrated -- https://main.php.net/note/delete/126214/integrated
Del: useless -- https://main.php.net/note/delete/126214/useless
Del: bad code -- https://main.php.net/note/delete/126214/bad+code
Del: spam -- https://main.php.net/note/delete/126214/spam
Del: non-english -- https://main.php.net/note/delete/126214/non-english
Del: in docs -- https://main.php.net/note/delete/126214/in+docs
Del: other reasons-- https://main.php.net/note/delete/126214
Reject -- https://main.php.net/note/reject/126214
Search -- https://main.php.net/manage/user-notes.php