PHP Tutorial PHP Advanced PHP Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

PHP - Array rsort() Function



The PHP Array rsort() function is used to sort an indexed array in descending order. The function has one optional parameter which can be used to specify sorting type. Please see below for more details.

Syntax

rsort(array, sorting_type)

Parameters

array Required. Specify the indexed array to sort
sorting_type Optional. There are six sorting types which are mentioned below:
  • 0 or SORT_REGULAR - Default. Compare items normally.
  • 1 or SORT_NUMERIC - Compare items numerically.
  • 2 or SORT_STRING - Compare items as strings.
  • 3 or SORT_LOCALE_STRING - Compare items as strings, based on current locale.
  • 4 or SORT_NATURAL - Compare items as strings using natural ordering.
  • 5 or SORT_FLAG_CASE - Compare items as strings and case-insensitive.

Note: The | (bitwise operator) can be used to specify more than one sorting type, for example - 4|5.


Return Value

Returns TRUE on success and False in failure.

Exceptions

NA.

Example:

In the example below, rsort() function is used to sort an indexed array called MyArray in descending order.

<?php
$MyArray = array("Marry", "John", "Jo", "Kim", "Adam");
rsort($MyArray);

print_r($MyArray);
?>

The output of the above code will be:

Array
(
    [0] => Marry
    [1] => Kim
    [2] => John
    [3] => Jo
    [4] => Adam
)

❮ PHP Array functions

5