我有這個簡單的橄欖球隊陣:PHP數組usort功能
Array
(
[0] => Array
(
[name] => MANCHESTER
[pts] => 8
[gd] => 5
)
[1] => Array
(
[name] => BOURNEMOUTH
[pts] => 3
[gd] => 2
)
[2] => Array
(
[name] => STOKE CITY
[pts] => 2
[gd] => 4
)
[3] => Array
(
[name] => LIVERPOOL
[pts] => 3
[gd] => 5
)
[4] => Array
(
[name] => ARSENAL
[pts] => 9
[gd] => 1
)
)
- 的名字 - 是球隊的名字
- 點 - 各隊的總積分
- GD-是各隊的淨勝球
我想通過團隊首先pts
排序,那麼如果我們通過具有相同的pts
,排序。
只有點的排序,我們有:
function sortByOrder($a, $b){
return $a['pts'] - $b['pts'];
}
usort($this_is_my_array, 'sortByOrder');
結果數組:
ARSENAL (pts:9, gd:1)
MANCHESTER (pts:8, gd:5)
BOURNEMOUTH (pts:3, gd:2)
LIVERPOOL (pts:3, gd:5)
STOKE CITY (pts:2, gd:4)
但BOURNEMOUTH
& LIVERPOOL
我們pts
重複,所以我們需要有這樣的結果:
ARSENAL (pts:9, gd:1)
MANCHESTER (pts:8, gd:5)
LIVERPOOL (pts:3, gd:5)
BOURNEMOUTH (pts:3, gd:2)
STOKE CITY (pts:2, gd:4)
請檢查我的答案.. –
@HaykMakyan _(FYI:您可以接受或給予好評這些答案,確實能幫助你的一個。)_ –