rsort
Sorts an array in descending order.
Syntax
rsort(array &$array, int $flags = SORT_REGULAR): trueParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array | The array being sorted. |
| $flags | int | Optional. The comparison mode. |
Return value
trueAlways `true`; the array itself is modified.
Examples
From largest to smallest:
<?php
$numbers = [3, 10, 1];
rsort($numbers);
echo implode(', ', $numbers);Output:
10, 3, 1The top three scores:
<?php
$scores = [70, 95, 82, 60, 91];
rsort($scores);
echo implode(', ', array_slice($scores, 0, 3));Output:
95, 91, 82Notes
The keys are replaced with numbers from zero.
See also
Related recipes
In the tutorial
- Sorting arrays — Sorting in PHP — sort, rsort, asort, ksort, a custom rule with usort and the spaceship operator.