W3docs

PHP Function rsort()

In this article, we will be discussing the PHP function array_rsort(), its syntax, parameters, and how it works. We will also provide some practical examples of

In this article, we will be discussing the PHP function rsort(), its syntax, parameters, and how it works. We will also provide some practical examples of using this function in real-world scenarios.

Introduction

Sorting is an essential operation when it comes to working with arrays in PHP. The rsort() function is used to sort an array in reverse order according to its values. It is a built-in function in PHP and can be used in a variety of scenarios where sorting arrays is required.

Syntax

The syntax for the rsort() function is as follows:

The syntax for the rsort() function in PHP

rsort($array, $flags);

The function takes two parameters: $array and $flags. The $array parameter is mandatory and represents the array that needs to be sorted. The $flags parameter is optional and represents the sorting behavior. It can take the following values:

  • SORT_REGULAR: Compare items normally (don't change types)
  • SORT_NUMERIC: Compare items numerically
  • SORT_STRING: Compare items as strings
  • SORT_LOCALE_STRING: Compare items as strings based on the current system locale settings.

How it Works

The rsort() function works by comparing the values in the array and sorting them in reverse order. It compares each value with the next one in the array and swaps them if they are not in the correct order. This process continues until the entire array is sorted.

Note that rsort() modifies the original array in-place and returns a boolean (true on success, false on failure). It also reindexes numeric keys sequentially after sorting.

Practical Examples

Let's take a look at some practical examples of using the rsort() function.

Example 1 - Sorting Numeric Values

Example of Sorting Numeric Values in PHP

<?php

$numbers = [10, 5, 8, 20, 15];
rsort($numbers, SORT_NUMERIC);
print_r($numbers);

Output:


Array
(
    [0] => 20
    [1] => 15
    [2] => 10
    [3] => 8
    [4] => 5
)

Example 2 - Sorting String Values

Example of Sorting String Values in PHP

<?php

$fruits = ["apple", "banana", "cherry", "date", "elderberry"];
rsort($fruits, SORT_STRING);
print_r($fruits);

Output:


Array
(
    [0] => elderberry
    [1] => date
    [2] => cherry
    [3] => banana
    [4] => apple
)

Example 3 - Sorting Mixed Values

Example of Sorting Mixed Values in PHP

<?php

$mix = ["apple", 10, "banana", 5, "cherry", 8];
rsort($mix, SORT_REGULAR);
print_r($mix);

Output:


Array
(
    [0] => cherry
    [1] => banana
    [2] => apple
    [3] => 10
    [4] => 8
    [5] => 5
)

Conclusion

In this article, we have discussed the rsort() function in PHP, covering its syntax, parameters, and behavior. We also provided practical examples of sorting numeric, string, and mixed arrays.

If you need to sort an array in reverse order according to its values, rsort() is a reliable built-in tool. By understanding its in-place modification behavior and available flags, you can efficiently handle array sorting in your PHP projects.

Practice

Practice

What does the rsort function do in PHP?