我搜索过这个,但似乎无法找到确切的答案.我想使用array_multisort基于3个数组中的数值同时对3个数组进行排序.基本上我想制作一个类似于NFL/NHL排名等的"排名"表.我有3个数组,tempIDs(字符串),tempWins(数字),tempWinPercentage(数字).我需要首先根据胜利同时对所有3个进行排序,然后如果有平局,则赢得百分比.
我似乎无法使用array_multisort来处理超过2个数组,所以当他们说它可以使用"几个"数组时,我可能会误解这些术语.谢谢!
你应该有这样的数据数组:
$data = array(
0 => array(
'tempIDs' => 'something',
'tempWins' => 10,
'tempWinPercentage' => 50,
),
1 => array(
'tempIDs' => 'something else',
'tempWins' => 10,
'tempWinPercentage' => 60,
),
3 => array(
'tempIDs' => 'something more',
'tempWins' => 20,
'tempWinPercentage' => 50,
),
);
Run Code Online (Sandbox Code Playgroud)
然后使用排序此数组 usort($data, 'my_sort_cb')
你的回调方法应首先比较tempWins,如果它们相等,则比较tempWinPercentages:
function my_sort_cb($a, $b) {
if ($a['tempWins'] > $b['tempWins']) return 1;
if ($a['tempWins'] < $b['tempWins']) return -1;
if ($a['tempWinPercentage'] > $b['tempWinPercentage']) return 1;
if ($a['tempWinPercentage'] < $b['tempWinPercentage']) return -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(这可以缩短)