PHP rsort() Function – Reverse Sorting an Array

The article explains PHP’s rsort() function, which sorts an array in reverse order, details its parameters and return values, and provides a complete example showing how to sort a fruit list and output the sorted elements with their indices.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP rsort() Function – Reverse Sorting an Array

The PHP rsort() function sorts an array in reverse order, from highest to lowest.

Parameters array – The input array passed by reference. sort_flags (optional) – Flags that modify sorting behavior, same as those used by sort(). Default is SORT_REGULAR.

Return value

Returns TRUE on success, or FALSE on failure.

Example

<?php
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val
";
}
?>

After calling rsort(), the $fruits array is sorted in reverse alphabetical order, and the script outputs:

0 = orange
1 = lemon
2 = banana
3 = apple
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend-developmentexample-codearray sortingrsort
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.