W3docs

Mastering arsort() in PHP: Sorting Arrays in Descending Order by Value

Learn how to use PHP's arsort() function to sort associative arrays in descending order by value while preserving key associations, with examples.

arsort() sorts an associative array in descending order by value while keeping each value tied to its original key. The "a" stands for associative (key associations are preserved) and the "r" stands for reverse (largest value first). This page covers its syntax, the optional sort flags, runnable examples, and how arsort() differs from the other PHP sorting functions.

Reach for arsort() whenever the keys carry meaning you must not lose — a map of product names to prices, usernames to scores, or categories to counts — and you want the highest values at the top.

Syntax

arsort(array &$array, int $flags = SORT_REGULAR): true
  • $array — the array to sort. It is passed by reference, so arsort() rearranges the array in place rather than returning a new one. The function itself returns true.
  • $flags — optional. Controls how values are compared. Defaults to SORT_REGULAR.

Sort flags

The $flags argument changes how two values are compared:

  • SORT_REGULAR — compare items normally, without changing their types (the default).
  • SORT_NUMERIC — compare items as numbers.
  • SORT_STRING — compare items as strings.
  • SORT_NATURAL — compare strings using a "natural order" algorithm (so "img10" sorts after "img2").
  • SORT_FLAG_CASE — combine with SORT_STRING or SORT_NATURAL using | to make string comparison case-insensitive.

Choosing the right flag matters: with the default SORT_REGULAR, the numeric strings "10" and "9" are compared numerically, but in a mixed array results can be surprising. Pick SORT_NUMERIC or SORT_STRING explicitly when the value types are known.

Sorting an associative array

php— editable, runs on the server

This code will output:

orange - 3
apple - 2
banana - 1

The values are now ordered from largest to smallest, and critically each fruit name still points to its own count — arsort() preserved the key/value pairing. This is exactly what makes it suitable for things like leaderboards or price lists.

Sorting with a flag

php— editable, runs on the server

This code will output:

1 - 5
4 - 4
0 - 3
2 - 2
3 - 1

The $numbers array is sorted in descending order by value using SORT_NUMERIC. Notice how the original numeric keys move with their values instead of being reindexed 0, 1, 2…. If you do not need to preserve those keys, use rsort() instead, which re-numbers the array.

arsort() vs. the other sorting functions

PHP's array sort functions form a consistent family. Knowing which one to use comes down to two questions: sort by value or by key? and preserve keys or not?

FunctionSorts byOrderKeeps key association
asort()valueascendingyes
arsort()valuedescendingyes
sort()valueascendingno (reindexed)
rsort()valuedescendingno (reindexed)
ksort()keyascendingyes
krsort()keydescendingyes

So arsort() is the "descending, by value, keep keys" member of the family — it is the mirror image of asort().

Common gotchas

  • It returns true, not the sorted array. Writing $sorted = arsort($array); sets $sorted to true. The sort happens on the original $array in place.
  • Numeric keys are not reindexed. That is the whole point of arsort(). If you want a clean 0, 1, 2… sequence after sorting, use rsort().
  • Comparing mixed types can give unexpected order under SORT_REGULAR. Pass an explicit flag when your array mixes strings and numbers.
  • For custom comparison logic (sorting objects, or by a computed key) reach for uasort(), which lets you supply your own comparison callback while preserving keys.

Conclusion

arsort() sorts an array in descending order by value while keeping every key bound to its value, making it ideal for ranked, label-carrying data such as scores or prices. Remember that it sorts in place and returns true, and pick a sort flag that matches your data types. When you need a different combination of order, key-vs-value, or key preservation, the related functions in the table above cover every case.

Practice

Practice
What is the functionality of the arsort() function in PHP?
What is the functionality of the arsort() function in PHP?
Was this page helpful?