对数组进行排序并保留键的值
我有一个数组,看起来像这样:
Array
(
[team1] => Array
(
[points] => 10
[players] => Array
(
...
)
)
[team2] => Array
(
[points] => 23
[players] => Array
(
...
)
)
... many more teams
)
我想按每支球队的积分对球队进行排序。我试过这个:
function sort_by_points($a,$b)
{
if ($a['points']==$b['points']) return 0;
return ($a['points']<$b['points'])?1:-1;
}
usort($this->wordswithdata, "sortbycount");
但这种方法会覆盖包含 teamname 的键并返回:
Array
(
[0] => Array
(
[points] => 23
[players] => Array
(
...
)
)
[1] => Array
(
[points] => 10
[players] => Array
(
...
)
)
etc...
)
有没有办法在不覆盖团队名称作为数组键的情况下对数组进行排序?