note 124266 added to function.max

From: Date: Mon, 07 Oct 2019 08:01:25 +0000
Subject: note 124266 added to function.max
Groups: php.notes 
Request: Send a blank email to php-notes+get-212543@lists.php.net to get a copy of this message
In response to: keith at bifugi dot com If you want to find the specific key(s) that match the maximum value in an array where the values may be duplicated, you can loop through and perform a simple check: <?php # Example Array $a = array( 'key1' => 100, 'key2' => 90, 'key3' => 100, 'key4' => 90, ); # First get the maximum value $max = max($a); # Create a new array containing all keys which have the max value foreach($a as $key => $val) { if($val === $max) $b[] = $key; } # If you want a string list, just do this $b = implode(' ', $b); ?> This produces consistent results and will scale well in terms of performance, whereas functions like array_search and array_flip can lead to degraded performance when dealing with large amounts of data. ---- Server IP: 208.43.231.9 Probable Submitter: 82.199.157.54 (proxied: 73.205.40.141) ---- Manual Page -- http://php.net/manual/en/function.max.php Edit -- https://master.php.net/note/edit/124266 Del: integrated -- https://master.php.net/note/delete/124266/integrated Del: useless -- https://master.php.net/note/delete/124266/useless Del: bad code -- https://master.php.net/note/delete/124266/bad+code Del: spam -- https://master.php.net/note/delete/124266/spam Del: non-english -- https://master.php.net/note/delete/124266/non-english Del: in docs -- https://master.php.net/note/delete/124266/in+docs Del: other reasons-- https://master.php.net/note/delete/124266 Reject -- https://master.php.net/note/reject/124266 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#212543) next »