I'm having some difficulties sorting a simple array that looks like this:
array(4) {
[32]=>
string(1) "1"
[34]=>
string(1) "2"
[35]=>
string(1) "1"
[33]=>
string(1) "0"
}
I just want to sort it by the index so it would look like this:
array(4) {
[32]=>
string(1) "1"
[33]=>
string(1) "0"
[34]=>
string(1) "2"
[35]=>
string(1) "1"
}
I tried using sort($votes);
but this seems to remove the index and my array looks like this afterwards:
array(4) {
[0]=>
string(1) "0"
[1]=>
string(1) "1"
[2]=>
string(1) "1"
[3]=>
string(1) "2"
}
So what would be the best way of sorting this array so that the index is still the same, but sorted?
You want to use ksort()
, which sorts an array by its keys.
ksort
Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.