When writing PHP applications, you may need to sort values descending while preserving the keys associated with each value. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The arsort() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.
What Is arsort() in PHP?
Sort values descending while preserving the keys associated with each value.
Its syntax is:
arsort(array &$array, int $flags = SORT_REGULAR): true
Parameters used by this operation:
&$array— The collection being examined or updated.$flags— An optional flag that controls comparison or processing behavior.
The original array changes and its key associations remain intact. Choose SORT_NUMERIC when values are numeric.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$scores = ["Maya" => 92, "Noah" => 80, "Lina" => 98];
arsort($scores, SORT_NUMERIC);
print_r($scores);
Output:
Array
(
[Lina] => 98
[Maya] => 92
[Noah] => 80
)
The output shows the result of the first call. Check the printed keys and values as well as their order.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$prices = ["A" => 19.5, "B" => 30, "C" => 11];
arsort($prices, SORT_NUMERIC);
print_r($prices);
Output:
Array
(
[B] => 30
[A] => 19.5
[C] => 11
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Produce a leaderboard of named members ordered from high score to low score
Produce a leaderboard of named members ordered from high score to low score. Save the code in a PHP file and adapt the sample data to your project.
<?php
$agentScores = ["Alice" => 90, "Bob" => 75, "Cara" => 98];
arsort($agentScores, SORT_NUMERIC);
foreach ($agentScores as $agent => $score) { echo "$agent: $score" . PHP_EOL; }
Output:
Cara: 98
Alice: 90
Bob: 75
The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.
Important Behavior and Edge Cases
The original array changes and its key associations remain intact. Choose SORT_NUMERIC when values are numeric.
- Test the operation with empty or missing input and decide how your application should respond.
- Read the return value independently from any changes to variables passed by reference.
- Validate untrusted input before using the result for permissions, database changes, or user-visible output.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
arsort() | Sort values descending while preserving the keys associated with each value |
| Related functions | rsort() discards existing keys; asort() sorts ascending. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing key sorting with value sorting | Check whether the comparison applies to keys or values before selecting the sort method. |
| Expecting an unchanged collection | The sorting operation updates the array or collection in place; copy it first when you need the original. |
| Ignoring comparison rules | The original array changes and its key associations remain intact. Choose SORT_NUMERIC when values are numeric. |
Key Takeaways
arsort()helps you sort values descending while preserving the keys associated with each value.- The original array changes and its key associations remain intact. Choose SORT_NUMERIC when values are numeric.
- rsort() discards existing keys; asort() sorts ascending.
- Use the practical example as a starting point, but validate real application inputs.
arsort() is useful when you need to sort values descending while preserving the keys associated with each value. Use its documented rules and the examples above to choose the right behavior for your data.