When I was testing a bit for fun I found this weird irregularity in the PHP asort()
function. When executing the following code:
$array = array(
"Player 1" => 5,
"Player 2" => 4,
"Player 3" => 4,
"Player 4" => 4,
"Player 5" => 4
);
asort($array);
print_r($array);
I would expect to see "Player 1" be moved to the bottom, and nothing more. However, the "Player 2" and "Player 3" also switch resulting in:
Array
(
[Player 3] => 4 // <- Swapped
[Player 2] => 4 // <- Swapped
[Player 4] => 4
[Player 5] => 4
[Player 1] => 5
)
When I, however, do the reverse of:
$array = array(
"Player 1" => 4,
"Player 2" => 4,
"Player 3" => 4,
"Player 4" => 4,
"Player 5" => 3
);
asort($array);
print_r($array);
I get the expected result where only "Player 5" gets put at the start resulting in:
Array
(
[Player 5] => 3
[Player 1] => 4
[Player 2] => 4 // <- Not swapped
[Player 3] => 4 // <- Not swapped
[Player 4] => 4
)
My question here is how does this function deal with these duplicates and why do 2 and 3 swap in the first example, but not in the second?