简体   繁体   中英

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.

The two keys I need to sort on are deal_score DESC and 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

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.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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