arsort
(PHP 4, PHP 5, PHP 7, PHP 8)
Sort an array in descending order and maintain index association
Description
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
arrayThe input array.
Return Values
Always
Changelog
| Version | Description |
|---|
Examples
arsort() example
<?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:
a = orange
d = lemon
b = banana
c = appleThe fruits have been sorted in reverse alphabetical order, and the index associated with each element has been maintained.