PHP rsort Function
last modified March 13, 2025
The PHP rsort
function sorts an array in descending order. It
modifies the original array and returns a boolean indicating success.
Basic Definition
The rsort
function sorts an array by values in reverse order.
It maintains index association for associative arrays but resets numeric keys.
Syntax: rsort(array &$array, int $flags = SORT_REGULAR): bool
. The
function returns true on success, false on failure. The array is passed by
reference.
Basic rsort Example
This demonstrates sorting a simple numeric array in descending order.
<?php $numbers = [3, 1, 4, 1, 5, 9, 2, 6]; rsort($numbers); print_r($numbers);
The array is sorted from highest to lowest value. The original array is modified, and numeric keys are reindexed starting from 0.
Sorting Strings with rsort
The rsort
function can also sort string arrays alphabetically
in reverse order.
<?php $fruits = ["apple", "Orange", "banana", "cherry"]; rsort($fruits); print_r($fruits);
Strings are sorted in reverse alphabetical order. Note that uppercase letters come before lowercase in ASCII order, affecting the sort result.
Using Sorting Flags
The second parameter allows specifying sorting behavior with various flags.
<?php $mixed = ["10", 2, "1", 20]; rsort($mixed, SORT_NUMERIC); print_r($mixed);
Using SORT_NUMERIC
treats values as numbers during comparison.
String numbers are converted to numeric values for proper numerical sorting.
Associative Array Behavior
With associative arrays, rsort
maintains value-key association
but resets numeric keys.
<?php $prices = [ "apple" => 1.2, "banana" => 0.5, "orange" => 0.8 ]; rsort($prices); print_r($prices);
The values are sorted in descending order, but string keys are lost. Only the values remain with new numeric indices.
Case-Insensitive Sorting
Combine rsort
with strcasecmp
for case-insensitive
sorting of string arrays.
<?php $words = ["Apple", "banana", "cherry", "apricot"]; rsort($words, SORT_STRING | SORT_FLAG_CASE); print_r($words);
The SORT_FLAG_CASE
flag makes the sorting case-insensitive.
"Apple" and "apricot" are properly ordered despite different cases.
Best Practices
- Preserve Keys: Use
arsort
to keep key-value associations. - Memory Usage: Be aware
rsort
modifies the original array. - Custom Sorting: For complex sorts, consider
usort
. - Large Arrays: Test performance with very large datasets.
Source
This tutorial covered the PHP rsort
function with practical
examples showing its usage for sorting arrays in descending order.
Author
List all PHP Array Functions.