Note! This question is outdated (thanks mickmackusa).
Edit: This question seems to be a duplicate of Preserve key order (stable sort) when sorting with PHP's uasort
Someone has been using arsort()
to sort an array parsed from HTTP_ACCEPT_LANGUAGE
under the assumption that it is a stable sort. But it's not: https://bugs.php.net/bug.php?id=53553. Now I have a bug and I am a bit at a loss how to fix the bug without resorting to hacks.
I have this header from a mobile client:
HTTP_ACCEPT_LANGUAGE: de-CH, en-US
and this gets parsed to:
Array (
[de-CH] => 1
[en-US] => 1
)
After parsing arsort($array, SORT_NUMERIC)
is used to sort the languages corresponding to their q values. But because German and English has the same q value, arsort()
swaps German and English. How can I sort the array so that the insertion order is preserved?