简体   繁体   English

对多个键进行多维数组排序

[英]Multidimensional array sort on multiple keys

Hi I've been using the array_sort() function found here for some time to sort results from multiple APIs but now I have the need to sort by two keys simultaneously. 嗨,我一直在使用这里找到的array_sort()函数对多个API的结果进行排序,但是现在我需要同时按两个键进行排序。

The two keys I need to sort on are deal_score DESC and date_start DESC 我需要排序的两个键是deal_score DESC和date_start DESC

The properties of this array are as follows. 该数组的属性如下。 Record 2 has the highest deal_score so should come first Records 0 and 1 have the same deal_score but date_start is higher on record 1 so the final order of results should be 2, 1, 0 记录2具有最高的deal_score因此应排在第一位记录0和1具有相同的deal_scoredate_start在记录1上较高,因此结果的最终顺序应为date_start

Here's an example array which has been trimmed down for readability. 这是一个为简化可读性而修剪的示例数组。

       [0] => Array
            (
                [db_id] => 414314
                [date_start] => 2012-04-17
                [deal_score] => 81.3
                [deal_statements] => Array
                (
                    [0] => 49.85
                    [1] => 2.11
                )
            )

        [1] => Array
            (
                [db_id] => 414409
                [date_start] => 2012-04-20
                [deal_score] => 81.3
                [deal_statements] => Array
                (
                    [0] => 28.2
                    [1] => 21.41
                )
            )

        [2] => Array
            (
                [db_id] => 1345923
                [date_start] => 2012-04-17
                [deal_score] => 85
                [deal_statements] => Array
                (
                    [0] => 18.1
                    [1] => 22.16
                )
            )

Any help on this will be greatly appreciated. 任何帮助,将不胜感激。

sth. 某事 like this should do: 这样应该做:

foreach ($data as $key => $row) {
    $score[$key] = $row['deal_score'];
    $dates[$key] = $row['date_start'];
}

array_multisort($score, SORT_ASC, $dates, SORT_ASC, $data);

3rd. 第三名 example on http://php.net/manual/en/function.array-multisort.php pretty much explains it. http://php.net/manual/en/function.array-multisort.php上的示例对此进行了解释。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM