我有一个看起来像这样的数组:
$this->wordswithdata = [
'team1' => [
'points' => 10,
'players' => [],
],
'team2' => [
'points' => 23,
'players' => []
]
];
我想按每个团队拥有的积分从高到低对团队进行排序。我尝试过这样做:
function sort_by_points($a,$b)
{
if ($a['points'] == $b['points']) return 0;
return ($a['points'] < $b['points']) ? 1 : -1;
}
usort($this->wordswithdata, "sortbycount");
但这种方法会覆盖包含团队名称的键,并返回:
[
0 => [
'points' => 23,
'players' => []
],
1 => [
'points' => 10,
'players' => [],
]
]
有没有办法在不失去团队名称作为数组键的情况下对数组进行排序?
<=>
代替减法。请参考此相关问题的答案。 - mbomb007