note 78849 deleted from function.max by danbrown
| From: | danbrown@php.net | Date: | Tue, 22 Mar 2011 14:00:20 +0000 |
| Subject: | note 78849 deleted from function.max by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178085@lists.php.net to get a copy of this message | ||
Note Submitter: mick at wireframe dot com
----
There are a couple of things you can do for cleaner code if you want the keys returned from the
array. I am not sure how they each impact performance, but the visual readability is more beneficial
for me -- your mileage may vary.
In the first example keys and max value is returned:
<?php
// First, let's pretend we have an array like this:
$Some_Array = array(
'john' => 40,
'susan' => 40,
'jane' => 24,
'michael' => 19,
'jimmy' => 38
);
function max_extract($My_Array) {
$Max_Value = max($My_Array);
return array_fill_keys(array_keys($My_Array, $Max_Value), $Max_Value);
} // ! max_extract()
?>
Or, if you are only interested in the keys and don't care what the max is:
<?php
function max_extract($My_Array) {
return array_keys($My_Array, max($My_Array));
// You can also array_flip() this is you want them as keys
// or if some other reason makes this relevant/needed.
} // ! max_extract()
?>
There are many possible variations when using the search parameter of array_keys() along with other
array functions.