This solution is for usort[] with an easy-to-remember notation for multidimensional sorting. The spaceship operator is used, which is available from PHP 7.
usort[$in,function[$a,$b]{
return $a['first'] $b['first'] //first asc
?: $a['second'] $b['second'] //second asc
?: $b['third'] $a['third'] //third desc [a b swapped!]
//etc
;
}];
Examples:
$in = [
['firstname' => 'Anton', 'surname' => 'Gruber', 'birthdate' => '03.08.1967', 'rank' => 3],
['firstname' => 'Anna', 'surname' => 'Egger', 'birthdate' => '04.01.1960', 'rank' => 1],
['firstname' => 'Paul', 'surname' => 'Mueller', 'birthdate' => '15.10.1971', 'rank' => 2],
['firstname' => 'Marie', 'surname' => 'Schmidt ', 'birthdate' => '24.12.1963', 'rank' => 2],
['firstname' => 'Emma', 'surname' => 'Mueller', 'birthdate' => '23.11.1969', 'rank' => 2],
];
First task: Order By rank asc, surname asc
usort[$in,function[$a,$b]{
return $a['rank'] $b['rank'] //first asc
?: $a['surname'] $b['surname'] //second asc
;
}];
Second task: Order By rank desc, surname asc, firstmame asc
usort[$in,function[$a,$b]{
return $b['rank'] $a['rank'] //first desc
?: $a['surname'] $b['surname'] //second asc
?: $a['firstname'] $b['firstname'] //third asc
;
}];
Third task: Order By rank desc, birthdate asc
The date cannot be sorted in this notation. It is converted with strtotime.
usort[$in,function[$a,$b]{
return $b['rank'] $a['rank'] //first desc
?: strtotime[$a['birthdate']] strtotime[$b['birthdate']] //second asc
;
}];
Definition
This page explains how to sort a multidimensional array in PHP. This frequently happens when the array is the result of a database query.
The example array
Let's considere the following multidimensional array:
$array[] = array['name' => 'Dupont', 'age' => 72];
$array[] = array['name' => 'Albert', 'age' => 11];
$array[] = array['name' => 'Durand', 'age' => 56];
$array[] = array['name' => 'Michel', 'age' => 52];
$array[] = array['name' => 'Dupont', 'age' => 36];
$array[] = array['name' => 'Plutot', 'age' => 27];
Dupont | 72 |
Albert | 11 |
Durand | 56 |
Michel | 52 |
Dupont | 36 |
Plutot | 27 |
Sort by name ascending
We have an array of rows, but array_multisort[] requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.
$columns = array_column[$array, 'name'];
array_multisort[$columns, SORT_ASC, $array];
Here is the result:
Albert | 11 |
Dupont | 36 |
Dupont | 72 |
Durand | 56 |
Michel | 52 |
Plutot | 27 |
Sort by age descending
$columns = array_column[$array, 'age'];
array_multisort[$columns, SORT_DESC, $array];
Here is the result:
Dupont | 72 |
Durand | 56 |
Michel | 52 |
Dupont | 36 |
Plutot | 27 |
Albert | 11 |
Sort by name ascending, then by age descending
$columns_1 = array_column[$array, 'name'];
$columns_2 = array_column[$array, 'age'];
array_multisort[$columns_1, SORT_ASC, $columns_2, SORT_DESC, $array];
Here is the result:
Albert | 11 |
Dupont | 72 |
Dupont | 36 |
Durand | 56 |
Michel | 52 |
Plutot | 27 |
See also
- Add icons on your web pages
- CSS color table
- CSS styles for Rainbow code highlighter
- How to detect ad blockers on web sites?
- HTML drop-down list of citizenships.
- HTML five stars ranking
- How Bézier curves are described in SVG paths
- How to check if a number is prime in JavaScript
- How to get list of subdirectories in PHP
- How to slugify a string in JavaScript?
- How to slugify a string in PHP?
- In PHP, how to convert a date in French?
- Levenshtein distance in mySQL
- List of supported languages by Prism syntax highlighter
- Nice HTML checkbox
- PHP array of countries and citizenships.
- Pure CSS loader
- Replace HTML checkbox with images
- Simple SSE in PHP
- Sessions or cookies?
Last update : 11/25/2021