I have an array that looks like this and I want to sort it by the value ‘RECLABEL’ ascending and by value ‘TOSITE’ low to high.
Array
(
[0] => Array
(
[MAPID] => 1
[RECTYPE] => EARZ
[RECLABEL] => EARZ
[TOSITE] => 0
[ID] => 1
[COUNTER] => Array
(
[0] =>
)
)
[1] => Array
(
[MAPID] => 2
[RECTYPE] => USALTFUELS
[RECLABEL] => ALTFUELS
[TOSITE] => 0.24
[ID] => 80117
[COUNTER] => Array
(
[0] => 1
)
)
[2] => Array
(
[MAPID] => 2
[RECTYPE] => USALTFUELS
[RECLABEL] => ALTFUELS
[TOSITE] => 0.18
[ID] => 58715
[COUNTER] => Array
(
[0] => 1
)
)
)
The output:
$Array = Array
(
[2] => Array
(
[MAPID] => 2
[RECTYPE] => USALTFUELS
[RECLABEL] => ALTFUELS
[TOSITE] => 0.18
[ID] => 58715
[COUNTER] => Array
(
[0] => 1
)
)
[1] => Array
(
[MAPID] => 2
[RECTYPE] => USALTFUELS
[RECLABEL] => ALTFUELS
[TOSITE] => 0.24
[ID] => 80117
[COUNTER] => Array
(
[0] => 1
)
)
[0] => Array
(
[MAPID] => 1
[RECTYPE] => EARZ
[RECLABEL] => EARZ
[TOSITE] => 0
[ID] => 1
[COUNTER] => Array
(
[0] =>
)
)
);
my code is like this and it does seem to work. Anyone can help me? Thanks
function cmp(array $a, array $b) {
if (($cmp = strcmp($a['RECLABEL'], $b['RECLABEL'])) !== 0) {
return $cmp;
} else {
return $a['TOSITE'] - $b['TOSITE'];
}
}
usort($array, cmp (array $a, array $b)