note 109413 added to function.uksort

From: Date: Sun, 15 Jul 2012 12:52:19 +0000
Subject: note 109413 added to function.uksort
Groups: php.notes 
Request: Send a blank email to php-notes+get-189144@lists.php.net to get a copy of this message
Use closures if you want to sort by key AND value: <?php /* suppose you want to sort using keys AND values * * task: Sort this array with highest values first, but if two values * are equal then their keys should be in alphabetical order. * * solution: use closures. */ $stock = array( 'apple' => 4, 'grape' => 3, 'banana' => 3, 'pear' => 1, 'orange' => 1 ); uksort($stock, function ($a, $b) use ($stock) { if ($stock[$a] != $stock[$b]) return $stock[$b] - $stock[$a]; return strcmp($a, $b); }); print_r($stock); /* output: Array ( [apple] => 4 [banana] => 3 [grape] => 3 [orange] => 1 [pear] => 1 ) */ ?> ---- Server IP: 113.20.13.217 Probable Submitter: 60.240.75.5 ---- Manual Page -- http://www.php.net/manual/en/function.uksort.php Edit -- https://master.php.net/note/edit/109413 Del: integrated -- https://master.php.net/note/delete/109413/integrated Del: useless -- https://master.php.net/note/delete/109413/useless Del: bad code -- https://master.php.net/note/delete/109413/bad+code Del: spam -- https://master.php.net/note/delete/109413/spam Del: non-english -- https://master.php.net/note/delete/109413/non-english Del: in docs -- https://master.php.net/note/delete/109413/in+docs Del: other reasons-- https://master.php.net/note/delete/109413 Reject -- https://master.php.net/note/reject/109413 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#189144) next »