duangekui7451 2012-09-24 07:49 采纳率: 0%
浏览 31
已采纳

PHP数组按多个值排序

I have the following multi-dimensional array that I want to sort.

I want to sort the innermost arrays by total_points, then tiebraker1, 2 and 3

Example:

Array
(
        [1] => Array
                (
                        [1] => Array
                                (
                                        [userid] => 17
                                        [total_points] => 16
                                        [tiebraker1] => 1
                                        [tiebraker2] => 2
                                        [tiebraker3] => 1
                                )

                        [2] => Array
                                (
                                        [userid] => 29
                                        [total_points] => 16
                                        [tiebraker1] => 1
                                        [tiebraker2] => 2
                                        [tiebraker3] => 9
                                )
                )

        [2] => Array
                (
                        [1] => Array
                                (
                                        [userid] => 26
                                        [total_points] => 26
                                        [tiebraker1] => 2
                                        [tiebraker2] => 2
                                        [tiebraker3] => 4
                                )

                        [2] => Array
                                (
                                        [userid] => 17
                                        [total_points] => 26
                                        [tiebraker1] => 3
                                        [tiebraker2] => 2
                                        [tiebraker3] => 4
                                )
                )
)

Result:

Array
(
        [1] => Array
                (
                        [1] => Array
                                (
                                        [userid] => 29
                                        [total_points] => 16
                                        [tiebraker1] => 1
                                        [tiebraker2] => 2
                                        [tiebraker3] => 9
                                )               
                        [2] => Array
                                (
                                        [userid] => 17
                                        [total_points] => 16
                                        [tiebraker1] => 1
                                        [tiebraker2] => 2
                                        [tiebraker3] => 1
                                )

                )

        [2] => Array
                (
                        [1] => Array
                                (
                                        [userid] => 17
                                        [total_points] => 26
                                        [tiebraker1] => 3
                                        [tiebraker2] => 2
                                        [tiebraker3] => 4
                                )               
                        [2] => Array
                                (
                                        [userid] => 26
                                        [total_points] => 26
                                        [tiebraker1] => 2
                                        [tiebraker2] => 2
                                        [tiebraker3] => 4
                                )


                )
)

I tried using array_multisort but I cant configure it correctly.

Thanks in advance for your help!

  • 写回答

2条回答 默认 最新

  • duanjia4220 2012-09-24 08:16
    关注

    To use array_multisort you would need a different structure for your data. Specifically you would need to group by "score type" (or expressed mathematically, transpose the array). E.g. like this using your first example:

    array(5) {
        // $userid
        [0] => array(2) {
            [0] => 17
            [1] => 29
        }
    
        // $total_points
        [1] => array(2) {
            [0] => 16
            [1] => 16
        }
    
        // $tiebreaker1
        [2] => array(4) {
            [0] => 1
            [1] => 1
        }
    
        // $tiebreaker2
        [3] => array(2) {
            [0] => 2
            [1] => 2
        }
    
        // $tiebreaker3
        [4] => array(2) {
            [0] => 1
            [1] => 9
        }
    }
    

    Then you could use array_multisort() as follows:

    array_multisort($ar[1], SORT_DESC, SORT_NUMERIC,
                    $ar[2], SORT_DESC, SORT_NUMERIC,
                    $ar[3], SORT_DESC, SORT_NUMERIC,
                    $ar[4], SORT_DESC, SORT_NUMERIC,
                    $ar[0], SORT_ASC, SORT_NUMERIC);
    

    If you cannot change the structure of the array, you could use usort() instead and define the comparision criteria manually.

    function cmp($a, $b)
    {
        if ($a['total_points'] != $b['total_points']) {
            return ($a['total_points'] > $b['total_points']) ? -1 : 1;
        } elseif ($a['tiebreaker1'] != $b['tiebreaker1']) {
            return ($a['tiebreaker1'] > $b['tiebreaker1']) ? -1 : 1;   
        } elseif ($a['tiebreaker2'] != $b['tiebreaker2']) {
            return ($a['tiebraker2'] > $b['tiebreaker2']) ? -1 : 1;   
        } elseif ($a['tiebreaker3'] != $b['tiebreaker3']) {
            return ($a['tiebreaker3'] > $b['tiebreaker3']) ? -1 : 1;   
        } else {
            return 0;
        }
    }
    
    usort($array, "cmp");
    

    Disclaimer: I do not claim that my implementation of cmp is the most elegant one. But it should do the trick. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错