This question already has answers here:
Sort multidimensional array by multiple columns

(8 个回答)


去年关闭。




我有一个多维数组,看起来像:
$arr=Array
(
    [0] => Array
        (
            [0] => TEAM1
            [1] => 3
            [2] => 0
            [3] => 422.47
            [4] => 192.62
        )

    [1] => Array
        (
            [0] => TEAM2
            [1] => 2
            [2] => 1
            [3] => 402.14
            [4] => 210.70
        )

    [2] => Array
        (
            [0] => TEAM3
            [1] => 3
            [2] => 0
            [3] => 376.79
            [4] => 174.64
        )
)

5 列与团队名称、# 胜利、# 失败、# 得分、# 对抗得分相关。

我将如何按第 1 列(# Wins)(降序)、第 2 列(# Losses)(升序)和第 3 列(# of Pts For)(降序)对 $arr 进行排序

最佳答案

我找到了一个使用 array_multisort() 的解决方案

foreach ($arr as $key => $row) {
    $wins[$key] = $row[1];
    $losses[$key] = $row[2];
    $ptsfor[$key] = $row[3];
}
array_multisort($wins, SORT_DESC, $losses, SORT_ASC, $ptsfor, SORT_DESC, $arr);

关于PHP 基于多条件对多维数组进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32727447/

10-11 21:16