arsort()

arsort() — Sort an array in descending order and maintain index association.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Sorts array in place in descending order, such that its keys maintain their correlation with the values they are associated with.

Signature

arsort(array &$array, int $flags = SORT_REGULAR): true

Parameters

ParameterTypeWhat it takes
&$arrayarrayThe input array.
$flagsintSorting type flags: SORT_REGULAR - compare items normally; the details are described in the comparison operators section SORT_NUMERIC - compare items numerically SORT_STRING - compare items as strings SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale() SORT_NATURAL - compare items as strings using "natural ordering" like natsort() ...

Return value

Always returns true.

Example

A runnable snippet:

$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); arsort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; }

Output:

a = orange d = lemon b = banana c = apple

Changelog

VersionChange
8.2.0The return type is true now; previously, it was bool.

See also

sort(), asort()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page arsort() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.