Possible Duplicates:
How to sort a multi dimensional array in PHP alphabetically?
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.
How can I alter the above to sort this output (when in PHP array format, not JSON).
Thankful for all help!
Check PHP.net before asking here. A quick search would have turned up PHP's asort()
function: http://php.net/asort
Check out php array_multisort
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');