Reputation: 3315
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.
Upvotes: 1
Views: 2478
Reputation: 13867
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.
Upvotes: 4