note 119581 added to function.max
| From: | keith at bifugi dot com | Date: | Mon, 11 Jul 2016 16:36:44 +0000 |
| Subject: | note 119581 added to function.max | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205964@lists.php.net to get a copy of this message | ||
The simplest way to get around the fact that max() won't give the key is array_search:
<?php
$student_grades = array ("john" => 100, "sarah" => 90, "anne"
=> 100);
$top_student = array_search(max($student_grades),$student_grades); // john
?>
This could also be done with array_flip, though overwriting will mean that it gets the last max
value rather than the first:
<?php
$grades_index = array_flip($student_grades);
$top_student = $grades_index[max($student_grades)]; // anne
?>
To get all the max value keys:
<?php
$top_students = array_keys($student_grades,max($student_grades)); // john, anne
?>
----
Server IP: 72.52.91.14
Probable Submitter: 92.30.246.220
----
Manual Page -- http://php.net/manual/en/function.max.php
Edit -- https://master.php.net/note/edit/119581
Del: integrated -- https://master.php.net/note/delete/119581/integrated
Del: useless -- https://master.php.net/note/delete/119581/useless
Del: bad code -- https://master.php.net/note/delete/119581/bad+code
Del: spam -- https://master.php.net/note/delete/119581/spam
Del: non-english -- https://master.php.net/note/delete/119581/non-english
Del: in docs -- https://master.php.net/note/delete/119581/in+docs
Del: other reasons-- https://master.php.net/note/delete/119581
Reject -- https://master.php.net/note/reject/119581
Search -- https://master.php.net/manage/user-notes.php