PHP 数组按指定的键值排序
需求:按照用户的 rank 从小到到排序(PHP 按照二维数组指定的键值排序)。
$users = [
[ 'id' => 1, 'rank' => 3 ],
[ 'id' => 2, 'rank' => 1 ],
[ 'id' => 3, 'rank' => 2 ],
];
array_multisort(array_column($users, 'rank'), SORT_ASC, $users);
输出结果:
Array
(
[0] => Array
(
[id] => 2
[rank] => 1
)
[1] => Array
(
[id] => 3
[rank] => 2
)
[2] => Array
(
[id] => 1
[rank] => 3
)
)