note 15030 deleted from function.usort by crell
| From: | crell@php.net | Date: | Wed, 30 Mar 2022 22:18:50 +0000 |
| Subject: | note 15030 deleted from function.usort by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217820@lists.php.net to get a copy of this message | ||
Note Submitter: josh at wintelcom dot net
----
This lets you sort an associative multi-dimensional array by multiple key/field names. Much
similiar to the SQL clause ORDER BY. Enjoy.
<?php
function cmp ($a, $b) {
// Populate this array with your values...
// Below is the SQL equivalent of
// select * from blah ORDER BY date desc, type asc, name asc
$vals = array(
'date' => 'd',
'type' => 'a',
'name' => 'a'
);
while(list($key, $val) = each($vals)) {
if($val == "d") {
if ($a["$key"] > $b["$key"]) {
return -1;
}
if ($a["$key"] < $b["$key"]) {
return 1;
}
}
if($val == "a") {
if ($a["$key"] < $b["$key"]) {
return -1;
}
if($a["$key"] > $b["$key"]) {
return 1;
}
}
}
}
?>