How can I sort an array in PHP alphabetically? [duplicate]
Possible Duplicates:
How to sort a multi dimensional array in PHP alphabet开发者_如何学运维ically? PHP : Sort array alphabetically
I am developing an API using Codeigniter and Phils RESTserver. In this API I access a database that contains users.
I would like to sort these users before outputting them. How can I do this? I tried the below code but that does not work.
function sort_by_lastname($a, $b)
{
return strcmp($a['user']['basic']['lastname'], $a['user']['basic']['lastname']);
}
This is my data in JSON format.
http://pastie.org/2402372
How can I alter the above to sort this output (when in PHP array format, not JSON).
Thankful for all help!
function sort_by_lastname($a, $b) {
$a = trim($a['user']['basic'][0]['lastname']);
$b = trim($b['user']['basic'][0]['lastname']);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
uasort($array['contacts'],'sort_by_lastname');
Check out php array_multisort
http://php.net/manual/en/function.array-multisort.php
Check PHP.net before asking here. A quick search would have turned up PHP's asort()
function: http://php.net/asort
精彩评论