Created
July 25, 2018 13:21
-
-
Save Inzman/07fc6118eb5f834e7fc2f6cda785f3f5 to your computer and use it in GitHub Desktop.
Sort array by inner key
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 | |
| $a = Array( | |
| 1 => Array( | |
| 'name' => 'Peter', | |
| 'age' => 17 | |
| ), | |
| 0 => Array( | |
| 'name' => 'Nina', | |
| 'age' => 21 | |
| ), | |
| 2 => Array( | |
| 'name' => 'Bill', | |
| 'age' => 15 | |
| ), | |
| ); | |
| function compareByName($a, $b) { | |
| return strcmp($a["name"], $b["name"]); | |
| } | |
| usort($a, 'compareByName'); | |
| /* The next line is used for debugging, comment or delete it after testing */ | |
| print_r($a); | |
| __________________ | |
| usort($array, function($a, $b){ return strcmp($a["name"], $b["name"]); }); | |
| __________________ | |
| function cmp($a, $b) | |
| { | |
| return strcmp($a["name"], $b["name"]); | |
| } | |
| usort($array, "cmp"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment