Anda juga bisa mengurutkan array multi dimensi untuk beberapa nilai seperti
$arr = [
[
"name"=> "Sally",
"nick_name"=> "sal",
"availability"=> "0",
"is_fav"=> "0"
],
[
"name"=> "David",
"nick_name"=> "dav07",
"availability"=> "0",
"is_fav"=> "1"
],
[
"name"=> "Zen",
"nick_name"=> "zen",
"availability"=> "1",
"is_fav"=> "0"
],
[
"name"=> "Jackson",
"nick_name"=> "jack",
"availability"=> "1",
"is_fav"=> "1"
],
[
"name"=> "Rohit",
"nick_name"=> "rod",
"availability"=> "0",
"is_fav"=> "0"
],
];
dengan
usort($arr,function($a,$b){
$c = $b['is_fav'] - $a['is_fav'];
$c .= $b['availability'] - $a['availability'];
$c .= strcmp($a['nick_name'],$b['nick_name']);
return $c;
});
Output menggunakan print_r($arr)
:
Array
(
[0] => Array
(
[name] => Jackson
[nick_name] => jack
[availability] => 1
[is_fav] => 1
)
[1] => Array
(
[name] => David
[nick_name] => dav07
[availability] => 0
[is_fav] => 1
)
[2] => Array
(
[name] => Zen
[nick_name] => zen
[availability] => 1
[is_fav] => 0
)
[3] => Array
(
[name] => Rohit
[nick_name] => rod
[availability] => 0
[is_fav] => 0
)
[4] => Array
(
[name] => Sally
[nick_name] => sal
[availability] => 0
[is_fav] => 0
)
)
PS) menggunakan strcmp akan menjadi pilihan yang baik untuk membandingkan string.