I have following array
Array(
[0]= Array
(
[name]=>room
other paramters
)
[1]=Array (
[name]=>abc
and so on
)
)
I want to sort by name
i tried to use usort. but i am not able to get proper results
usort($array,'sort_by_name');
function sort_by_name($a,$b) {
return $a- $b;
}
Any idea?
Thanks
usort()is probably not what you need, and without showing the rest of the code we can only assume you're not using it correctly. Tryksort().ksortwould sort by the key, not the value. As in the0and1. @OPusortis correct and in your compare function just use something likereturn strcasecmp($a['name'], $b['name']);in your compare function. php already has a string comparison function that returns the proper values, you just need to reference the key.function sort_by_name($a,$b) { return strcmp($a['name'], $b['name']); }perhaps?