次のように、複数の値の多次元配列をソートすることもできます
$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"
],
];
と
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;
});
を使用して出力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)strcmpを使用すると、文字列を比較するのに適したオプションになります。