note 78849 added to function.max

From: Date: Tue, 30 Oct 2007 23:51:26 +0000
Subject: note 78849 added to function.max
Groups: php.notes 
Request: Send a blank email to php-notes+get-131836@lists.php.net to get a copy of this message
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. ---- Server IP: 209.41.74.194 Probable Submitter: 64.52.10.140 ---- Manual Page -- http://www.php.net/manual/en/function.max.php Edit -- https://master.php.net/note/edit/78849 Del: integrated -- https://master.php.net/note/delete/78849/integrated Del: useless -- https://master.php.net/note/delete/78849/useless Del: bad code -- https://master.php.net/note/delete/78849/bad+code Del: spam -- https://master.php.net/note/delete/78849/spam Del: non-english -- https://master.php.net/note/delete/78849/non-english Del: in docs -- https://master.php.net/note/delete/78849/in+docs Del: other reasons-- https://master.php.net/note/delete/78849 Reject -- https://master.php.net/note/reject/78849 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131836) next »