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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?