The arsort Function
The arsort function sorts an array
in descending order while preserving keys.
The function modifies the array itself.
Syntax
arsort(array &$array, int $flags = SORT_REGULAR): bool
Example
Let's sort an associative array in descending order:
<?php
$arr = [
'b' => 1,
'e' => 3,
'c' => 2,
'a' => 5,
'd' => 4,
];
arsort($arr);
var_dump($arr);
?>
Code execution result:
[
'd' => 4,
'e' => 3,
'c' => 2,
'b' => 1,
'a' => 5,
]
See Also
-
the
sortfunction,
which sorts in ascending order -
the
rsortfunction,
which sorts in descending order -
the
ksortfunction,
which sorts by keys in ascending order -
the
krsortfunction,
which sorts by keys in descending order -
the
asortfunction,
which sorts in ascending order while preserving keys -
the
arsortfunction,
which sorts in descending order while preserving keys -
the
natsortfunction,
which sorts using natural order algorithm -
the
natcasesortfunction,
which sorts using natural order algorithm case-insensitively -
the
usortfunction,
which sorts using a callback function -
the
uksortfunction,
which sorts by keys using a callback function -
the
uasortfunction,
which sorts using a callback function while preserving keys -
the
array_multisortfunction,
which sorts multiple arrays