PHP数组按多个值排序

时间:2012-09-24 07:49:21

标签: php multidimensional-array

我有以下想要排序的多维数组。

我想用total_points排序最里面的数组,然后是tiebraker1,2和3

示例:

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
                                )
                )
)

结果:

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
                                )


                )
)

我尝试使用array_multisort但我无法正确配置它。

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

要使用array_multisort,您需要一个不同的数据结构。具体而言,您需要按“分数类型”分组(或以数学方式表示,转置数组)。例如。像这样使用你的第一个例子:

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
    }
}

然后您可以使用array_multisort(),如下所示:

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);

如果无法更改数组的结构,可以使用usort()代替手动定义比较标准。

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");

免责声明:我并未声称cmp的实施是最优雅的。{但它应该做的伎俩。 :)

答案 1 :(得分:0)

来自PHP.net的文档:

<?php
$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?> 

在此示例中,排序后,第一个数组将转换为“10”,100,100,11,“a”(它按升序排序为字符串)。第二个将包含1,3,“2”,2,1(按数字排序,按降序排列)。

array(2) {
  [0]=> array(5) {
    [0]=> string(2) "10"
    [1]=> int(100)
    [2]=> int(100)
    [3]=> int(11)
    [4]=> string(1) "a"
  }
  [1]=> array(5) {
    [0]=> int(1)
    [1]=> int(3)
    [2]=> string(1) "2"
    [3]=> int(2)
    [4]=> int(1)
  }
}