Signature
arsort(array &$array, int $flags = SORT_REGULAR): truePurpose
arsort() sorts an array in place by its values, from largest to smallest, and keeps every key attached to its value. The name reads as "associative reverse sort". It is the right tool for leaderboards, vote counts, word frequencies and any other keyed data where the order matters but the keys must survive.
If you do not care about keys, rsort() gives the same value order but renumbers the keys. If you want to sort by key instead, look at krsort().
Signature
Parameters and defaults
$arrayis taken by reference, so the original variable is modified. You must pass a variable, not a literal array or a function result.$flagschanges how values are compared. The default isSORT_REGULAR, which compares values with PHP's normal comparison rules.
The supported flags are:
SORT_REGULARcompares items normally.SORT_NUMERICcompares items as numbers.SORT_STRINGcompares items as strings.SORT_LOCALE_STRINGcompares strings using the current locale, whichsetlocale()can change.SORT_NATURALcompares strings using natural ordering, likenatsort().SORT_FLAG_CASEcan be combined withSORT_STRINGorSORT_NATURALusing|for case-insensitive comparison.
Return value and failure cases
The function always returns true. Since PHP 8.2 the declared return type is true; before that it was documented as bool. The result is never a sorted copy: the sorted data lives in the variable you passed. Passing something that is not an array raises a TypeError.
Sorting also resets the array's internal pointer to the first element.
Example 1: a basic leaderboard
The keys are names and the values are scores. After sorting, the highest score comes first and each name still points to its own score.
$scores = ["ana" => 82, "ben" => 95, "cy" => 70];
arsort($scores);
echo json_encode($scores, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"ben":95,"ana":82,"cy":70}
Example 2: ties and the return value
When two values compare as equal, they keep their original relative order. This is guaranteed since PHP 8.0; earlier versions left it undefined. In the example below, green was before gold in the input and stays before it, and red stays before blue.
$votes = ["red" => 3, "green" => 5, "blue" => 3, "gold" => 5];
$result = arsort($votes);
var_dump($result);
echo json_encode($votes, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";bool(true)
{"green":5,"gold":5,"red":3,"blue":3}
Example 3: choosing a flag
The same numeric-looking strings sort differently depending on the flag. With the default flag, PHP treats numeric strings as numbers, so "100" is largest. With SORT_STRING, the comparison is character by character, so "9" beats "100", and "100" beats "10" because it is longer with the same prefix.
$items = ["a" => "10", "b" => "9", "c" => "100"];
$regular = $items;
$text = $items;
arsort($regular);
arsort($text, SORT_STRING);
echo json_encode($regular), "\n";
echo json_encode($text), "\n";{"c":"100","a":"10","b":"9"}
{"b":"9","c":"100","a":"10"}
Edge cases
- Empty arrays are fine. Nothing changes and the function still returns
true. - Integer keys are preserved too.
arsort()does not renumber them, which is the main difference fromrsort(). - Mixed types under
SORT_REGULARfollow PHP's general comparison rules, so mixing numbers, numeric strings and non-numeric strings can give results that are surprising. Pick an explicit flag when your data is not uniform. - Locale-based sorting with
SORT_LOCALE_STRINGdepends on the environment, so avoid it when you need reproducible output. - Nested arrays are compared as arrays under
SORT_REGULAR. If you need to sort by one field inside each element, useuasort()with a comparison callback.
Common mistakes
- Assigning the result. Writing
$sorted = arsort($data);storestrue, not an array. Callarsort($data);and then use$data. - Passing a temporary. Because the parameter is by reference,
arsort(getScores());is not valid for a function result. Store the value in a variable first. - Expecting a list. After sorting, the keys are not
0, 1, 2...in order. If you need a reindexed list, applyarray_values()afterwards. - Using the wrong function.
arsort()sorts by value. If you want the keys ordered, usekrsort(); if you want ascending values with keys kept, useasort(). - Ignoring the flag. Strings that look like numbers can sort numerically or alphabetically depending on
$flags, so state your intent explicitly.
Related functions
asort()sorts values ascending and keeps keys.sort()andrsort()sort values ascending or descending and reindex the keys.krsort()sorts by key in descending order.uasort()sorts by value using your own comparison function and keeps keys.array_multisort()sorts several arrays or dimensions at once.
Recap
Use arsort() when you want the biggest values first without losing the keys. It modifies the array in place, returns true, keeps equal elements in their original order, and accepts flags to control how values are compared. Remember to use the variable afterwards, not the return value.
Tested examples
$scores = ["ana" => 82, "ben" => 95, "cy" => 70];
arsort($scores);
echo json_encode($scores, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"ben":95,"ana":82,"cy":70}
$votes = ["red" => 3, "green" => 5, "blue" => 3, "gold" => 5];
$result = arsort($votes);
var_dump($result);
echo json_encode($votes, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";bool(true)
{"green":5,"gold":5,"red":3,"blue":3}
$items = ["a" => "10", "b" => "9", "c" => "100"];
$regular = $items;
$text = $items;
arsort($regular);
arsort($text, SORT_STRING);
echo json_encode($regular), "\n";
echo json_encode($text), "\n";{"c":"100","a":"10","b":"9"}
{"b":"9","c":"100","a":"10"}