note 23625 deleted from function.array-multisort by aidan

From: Date: Sun, 03 Oct 2004 10:59:09 +0000
Subject: note 23625 deleted from function.array-multisort by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-77812@lists.php.net to get a copy of this message
Note Submitter: kschnaitter@hotmail ---- It can be very useful to use array_keys(*) as one of the arguments. Example: you have an associative array of items and quantities, where the item is used as a key. Say you want to sort the array by descending quantity, and sort items with the same quantity in alphabetical order. Here is the code: <?php $arr["potato"] = 5; $arr["orange"] = 3; $arr["apple"] = 3; $arr["lemon"] = 4; $arr["onion"] = 5; $arr["grapefruit"] = 3; array_multisort ($arr, SORT_DESC, array_keys ($arr)); print_r ($arr); ?> Here is the output: Array ( [onion] => 5 [potato] => 5 [lemon] => 4 [apple] => 3 [grapefruit] => 3 [orange] => 3 )

« previous php.notes (#77812) next »