Created
April 17, 2012 18:09
-
-
Save kcrwfrd/2407899 to your computer and use it in GitHub Desktop.
Array Sorter Function - Sort by Nested Key
This file contains 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( | |
array('community_id'=>5, 'community_name'=>'Meadows'), | |
array('community_id'=>7, 'community_name'=>'Grosse Pointe'), | |
array('community_id'=>12, 'community_name'=>'Mountain View'), | |
array('community_id'=>6, 'community_name'=>'Seaside'), | |
); | |
function buildSorter($key) { | |
return function ($a, $b) use ($key) { | |
return strnatcmp($a[$key], $b[$key]); | |
} | |
} | |
usort($a, buildSorter('community_name')); | |
var_dump($a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment