php8.5
Home/ Manual/ array / functions/ arsort

arsort

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Sort an array in descending order and maintain index association

Description

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

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

This is used mainly when sorting associative arrays where the actual element order is significant.

Sort-unstable Reset-index

Parameters

array

The input array.

Return Values

Always

Changelog

VersionDescription

Examples

arsort() example

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

The above example will output:

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

The fruits have been sorted in reverse alphabetical order, and the index associated with each element has been maintained.

See Also

Source: reference/array/functions/arsort.xml · from the official PHP manual (php/doc-en)