Skip to content

Instantly share code, notes, and snippets.

@kcrwfrd
Created April 17, 2012 18:09
Show Gist options
  • Save kcrwfrd/2407899 to your computer and use it in GitHub Desktop.
Save kcrwfrd/2407899 to your computer and use it in GitHub Desktop.
Array Sorter Function - Sort by Nested Key
<?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