Monday, June 13, 2011

Sort an Array of Arrays in PHP

Want to know how to sort the code below by [name]?
Click me to navigate to the tutorial.
Array
(
[0] => Array
(
[id] => 1
[name] => Rob
)
[1] => Array
(
[id] => 4
[name] => Ultimate
)
[2] => Array
(
[id] => 8
[name] => Dog
)

eg:
//for array
function views($a,$b){
return strnatcmp($a['name'], $b['name']);//to reverse the sort swap the variables(letters)
}
//for stdClass object array
function viewsObj($a,$b){
return strnatcmp($a->name, $b->name);
}
usort($result,'views');
echo '
';
print_r($result);
echo '
';

No comments:

Post a Comment