note 23625 added to function.array-multisort
| From: | kschnaitter at hotmail dot php dot net | Date: | Wed, 24 Jul 2002 01:21:50 +0000 |
| Subject: | note 23625 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-33456@lists.php.net to get a copy of this message | ||
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:
$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
)
--
http://www.php.net/manual/en/function.array-multisort.php
http://master.php.net/manage/user-notes.php?action=edit+23625
http://master.php.net/manage/user-notes.php?action=delete+23625
http://master.php.net/manage/user-notes.php?action=reject+23625