I got a multidimensional array which may look like this:
$array[1][0] = "string";
$array[0][1] = "anotherstring";
$array[0][0] = "thirdstring";
$array[1][1] = "fourthstring";
I want to sort this array by keys so it looks like this:
$array[0][0] = "thirdstring";
$array[0][1] = "anotherstring";
$array[1][0] = "string";
$array[1][1] = "fourthstring";
At the moment I am using the following procedure:
ksort($array);
foreach ($array as $key => $value) {
ksort($value);
$array[$key] = $value;
}
This does work perfectly, but maybe there is a better (in-built) function to do this?
You can shorten your loop with:
ksort($array);
foreach($array as &$value) {
ksort($value);
}
Or use array_walk
:
ksort($array);
array_walk($array, fn (&$value) => ksort($value));