What the title says. Specifically if I have
$array1['name'] = 'zoo';
$array2['name'] = 'fox';
How can I determine that alphabetically $array2
's name should come above $array1
's?
What the title says. Specifically if I have
$array1['name'] = 'zoo';
$array2['name'] = 'fox';
How can I determine that alphabetically $array2
's name should come above $array1
's?
EDIT just realised values from different arrays, could array_merge first but not sure thats what you want
Use strcmp
. If the first argument to strcmp is lexographically smaller to the second, then the value returned will be negative. If both are equal, then it will return 0. And if the first is lexograpically greater than the second then a positive number will be returned.
nb. You probably want to use strcasecmp(string1,string2)
, which ignores case...
I often use natsort ( Natural Sort http://php.net/manual/en/function.natsort.php ), since I usually just want to preserve the array for later use anyway.
natsort($unsorted_array);
var_dump($usorted_array); // will now be sorted.