如何订购这个棘手的PHP关联数组?

How to order this tricky PHP associative array?

I have this associative array:

Array (
    [4] => 3
    [2] => 4
    [3] => 1
    [6] => 1
    [1] => 1
)

I need to order it by key with highest value, BUT I also need to keep the keys with the same values in their original order, so it needs to come out to:

Array (
    [2] => 4
    [4] => 3
    [3] => 1
    [6] => 1
    [1] => 1
    )

I cannot use arsort() because it rearranges the keys with the same value based on the key's numeric order, I'm really at a loss here! Any suggestions?

natsort to rescue:

$blub = array(4 => 3, 2 => 4, 3 => 1, 6 => 1, 1 => 1);
natsort($blub);
$blub = array_reverse($blub, true);

var_dump($blub);

This will always output:

array(5) { [2]=> int(4) [4]=> int(3) [3]=> int(1) [6]=> int(1) [1]=> int(1) }

natsort seems to be using a different sorting algorithm which luckily preserves the order when the values are the same as opposed to asort. Note however that natsort might be slightly slower than the other traditional sorting functions because of this.