I'm trying to sort an array by value descending keeping keys (arsort
), but if the values are equal I want it also sorted in order of keys ascending (ksort
).
I've been trying this:
ksort($array);
arsort($array);
But the ksort is not kept, and the keys get jumbled up again after the arsort
.
E.g. if my input array in:
$array[0] = 4;
$array[1] = 2;
$array[2] = 3;
$array[3] = 1;
$array[4] = 4;
I want to sort it so it ends like this:
$array[0] = 4;
$array[4] = 4;
$array[2] = 3;
$array[1] = 2;
$array[3] = 1;
NOT like this:
$array[4] = 4;
$array[0] = 4;
$array[2] = 3;
$array[1] = 2;
$array[3] = 1;
But the previous order of the keys appears to be disturbed by arsort
.