Current Location: Home> Latest Articles> Why do key names get lost after using rsort? How to solve this common issue?

Why do key names get lost after using rsort? How to solve this common issue?

gitbox 2025-06-17

In PHP, we often use various sorting functions to sort arrays. rsort() is one of the commonly used functions, which is used for sorting arrays in reverse order (from largest to smallest). However, many developers find that after using rsort(), the key names of the array are reset, and the original key names are lost. This can cause problems when working with associative arrays.

Why are key names lost?

rsort() is specifically designed for sorting indexed arrays (arrays with numeric keys), and it rebuilds the array indices, starting from 0 automatically. It does not retain the original key names because its purpose is to sort the values and generate continuous numeric indices.

In simple terms, rsort() sorts by value and reindexes the array, which is suitable for standard numeric arrays.

For example:

<?php
$arr = [
    &#039;a&#039; => 3,
    &#039;b&#039; => 1,
    &#039;c&#039; => 2
];
rsort($arr);
print_r($arr);
?>

The output is:

Array
(
    [0] => 3
    [1] => 2
    [2] => 1
)

As you can see, the original key names 'a', 'b', 'c' are completely gone and replaced by continuous numeric indices.


How to solve this problem?

If we need to sort an array with key names and keep the key names intact, we can consider the following methods:

1. Use arsort()

arsort() is a function that sorts associative arrays by value in reverse order while keeping the key names intact.

Example:

<?php
$arr = [
    &#039;a&#039; => 3,
    &#039;b&#039; => 1,
    &#039;c&#039; => 2
];
arsort($arr);
print_r($arr);
?>

Output:

Array
(
    [a] => 3
    [c] => 2
    [b] => 1
)

Here, the key names a, b, c are fully preserved.

2. Use uasort() for custom sorting

If you need a custom sorting rule, you can use uasort(), which also keeps the key names intact.

<?php
$arr = [
    &#039;a&#039; => 3,
    &#039;b&#039; => 1,
    &#039;c&#039; => 2
];
<p>uasort($arr, function($x, $y) {<br>
return $y <=> $x;  // Reverse order<br>
});</p>
<p>print_r($arr);<br>
?><br>

Output:

Array
(
    [a] => 3
    [c] => 2
    [b] => 1
)

3. Custom sorting example while retaining key names

If the array structure is more complex, you can extract key-value pairs, sort them, and then reconstruct the array.


Summary

  • rsort() loses key names because it reindexes the array, which is suitable for sorting simple indexed arrays.

  • If you need to preserve key names, you should use arsort() or uasort().

  • arsort() sorts values in reverse order while retaining key names.

  • uasort() allows for custom sorting logic, offering more flexibility.

Understanding the differences and uses of these functions will help you make your PHP array sorting more efficient and avoid issues with lost key names.


<?php
// Complete example based on the above content
<p>$arr = [<br>
'a' => 3,<br>
'b' => 1,<br>
'c' => 2<br>
];</p>
<p>// Incorrect example: rsort will lose key names<br>
rsort($arr);<br>
print_r($arr);</p>
<p>echo "\n";</p>
<p>// Correct example: use arsort to keep key names in reverse order<br>
$arr = [<br>
'a' => 3,<br>
'b' => 1,<br>
'c' => 2<br>
];<br>
arsort($arr);<br>
print_r($arr);</p>
<p>echo "\n";</p>
<p>// Custom sorting example using uasort<br>
$arr = [<br>
'a' => 3,<br>
'b' => 1,<br>
'c' => 2<br>
];<br>
uasort($arr, function($x, $y) {<br>
return $y <=> $x; // Reverse order<br>
});<br>
print_r($arr);<br>
?><br>

For more PHP array sorting tips, you can refer to gitbox.net/docs/php-arrays.