note 23625 modified in function.array-multisort by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:43:50 +0000 |
| Subject: | note 23625 modified in function.array-multisort by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79886@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:
<?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
)
--was--
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://php.net/manual/en/function.array-multisort.php