note 81531 deleted from function.max by danbrown
| From: | danbrown@php.net | Date: | Tue, 22 Mar 2011 13:59:30 +0000 |
| Subject: | note 81531 deleted from function.max by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178082@lists.php.net to get a copy of this message | ||
Note Submitter: stuff at necr0manzer dot de
----
mick at wireframe dot com's solution to finding the key for the highest value didn't work
for me, so I wrote one myself:
<?php
function max_key($array) {
foreach ($array as $key => $val) {
if ($val == max($array)) return $key;
}
}
$array = array(1, 2, 5, 7, 4);
echo max($array); // 7
echo max_key($array); // 3
?>
of course this also works with associative arrays, but it will only return a single result!