This question already has an answer here:
- sort array in DESC order 4 answers
I have an array like this
[Title 1] => 2
[Title 2] => 4
[Title 3] => 3
[Title 4] => 1
All I'm trying to do is sort the integer value in the array, to get an output like this:
[Title 2] => 4
[Title 3] => 3
[Title 1] => 2
[Title 4] => 1
I don't think it matters, but my array is created by the array_count_values function.
$array = array_count_values($originalarray);
I've looked through the manual on PHP: Array Sorting but can't get anything to work.
I've tried a few things that look like they might work.
$newarray = arsort($array);
This just outputs 1
same with rsort
.
The closest I've come is by using array_multisort by using it as such:
$array = array_count_values($originalarray);
$newarray = array_multisort($array, SORT_NUMERIC, SORT_DESC);
Now if I do a print_r($array)
it outputs like this:
[Title 2] => 4
[1] => 3
[Title 1] => 2
[2] => 1
Meaning it's, for some reason, and seemingly randomly, destroying the key value in the array and putting in place 1
, 2
, etc in place of Title #
This seems like it would be so easy, yet I've been trying multiple things without luck. Can anyone shine some light on this for me?
</div>