Computer >> Computer tutorials >  >> Programming >> PHP

rsort() function in PHP


The rsort() function sorts an array in reverse order. It returns TRUE on success and FALSE on failure.

Syntax

rsort(arr, flag)

Parameters

  • arr − The array to sort.

  • flag


    • 0 = SORT_REGULAR - Default. Compare items normally. Do not change types.

    • 1 = SORT_NUMERIC - Compare items numerically

    • 2 = SORT_STRING - Compare items as strings

    • 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale

    • 4 = SORT_NATURAL - Compare items as strings using natural ordering.

Return

The rsort() function returns TRUE on success and FALSE on failure.

Example

The following is an example −

<?php
$arr = array(30, 12, 76, 98, 45, 78);
rsort($arr);
print_r($arr);
?>

Output

The following is the output −

Array
(
[0] => 98
[1] => 78
[2] => 76
[3] => 45
[4] => 30
[5] => 12
)

Example

Let us see another example −

<?php
$arr = array("Tim", "Peter", "Anne", "Jacob", "Vera");
rsort($arr, SORT_STRING);
print_r($arr);
?>

Output

The following is the output −

Array
(
[0] => Vera
[1] => Tim
[2] => Peter
[3] => Jacob
[4] => Anne
)