1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?php $arr = [ ['name' => '张三', 'age' => 25, 'k_sort' => 3, 'recommend' => 2], ['name' => '李四', 'age' => 23, 'k_sort' => 3, 'recommend' => 1], ['name' => '王五', 'age' => 30, 'k_sort' => 5, 'recommend' => 2], ];
usort($arr, function ($a, $b) { $result = $b['k_sort'] - $a['k_sort']; if ($result == 0) { $result = $a['recommend'] <=> $b['recommend']; if ($result == 0) { $result = $b['age'] - $a['age']; } } return $result; });
print_r($arr);
运行结果: Array ( [0] => Array ( [name] => 王五 [age] => 30 [k_sort] => 5 [recommend] => 2 )
[1] => Array ( [name] => 李四 [age] => 23 [k_sort] => 3 [recommend] => 1 )
[2] => Array ( [name] => 张三 [age] => 25 [k_sort] => 3 [recommend] => 2 )
)
|