1

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

我想按 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 但我无法正确配置它。

在此先感谢您的帮助!

4

2 回答 2

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是最优雅的。但它应该可以解决问题。:)

于 2012-09-24T08:16:58.147 回答
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)
  }
}
于 2012-09-24T08:16:24.730 回答