I have a multidimensional array in php and I want to naturally sort the array based on the key value. The array in question:
array(27) {
["user1"]=>
array(1) {
["link"]=>
string(24) "http://twitch.tv/example"
}
["someotheruser"]=>
array(1) {
["link"]=>
string(24) "http://twitch.tv/example"
}
["anotheruser"]=>
array(1) {
["link"]=>
string(24) "http://twitch.tv/example"
}
}
So far, I have been trying to do a few things, but I'm out of luck. Using uksortwith natsort doesn't work, and I don't want to go as far as writing my own comparator for a natural sort order if I don't need to. I also tried to sort the keys individually, but that didn't help.
private function knatsort(&$array) {
$keys = array_keys($array);
natsort($keys);
$new_sort = array();
foreach ($keys as $keys_2) {
$new_sort[$keys_2] = $array[$keys_2];
}
$array = $new_sort;
return true;
}
source
share