-
-
Save grim-reapper/958e75c49cc0aa4aeea34e1967efce3d to your computer and use it in GitHub Desktop.
Custom sort multidimensional array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Array with data | |
| $teams = array | |
| ( | |
| array('name' => 'Maple Leafs', 'city' => 'Toronto'), | |
| array('name' => 'Canucks', 'city' => 'Vancouver'), | |
| array('name' => 'Senators', 'city' => 'Ottawa'), | |
| array('name' => 'Canadiens', 'city' => 'Montreal'), | |
| array('name' => 'Flames', 'city' => 'Calgary'), | |
| array('name' => 'Oilers', 'city' => 'Edmonton'), | |
| array('name' => 'Jets', 'city' => 'Winnipeg') | |
| ); | |
| // Custom sort multidimensional array | |
| usort($teams, function($a, $b) | |
| { | |
| return strnatcasecmp($a['city'], $b['city']); | |
| }); | |
| // Display sorted data | |
| print_r($teams); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment