w3resource

PHP: rsort() function

PHP: Sort an array in reverse order

This rsort() function is used to sort an array in reverse order (highest to lowest).

Note: If two members compare as equal, their relative order in the sorted array is undefined.

Version:

(PHP 4 and above)

Syntax:

rsort(array1, sort_flags )

Parameters:

Name Description Required /
Optional
Type
array1 The input array to sort. Required Array
sort_flags Sets the sorting behavior. Possible Sorting type flags.
SORT_REGULAR - Compare items normally.
SORT_NUMERIC - Compare items. numerically
SORT_STRING - Compare items as strings.
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
Optional Integer

Return value:

TRUE on success or FALSE on failure.

Value Type: Boolean.

Example:

<?php
$subject = array("Language", "English", "Math", "History");
rsort($subject);
foreach ($subject as $key => $val)
{
echo "subject[ " . $key . "] = " . $val . "<br />";
}
?>

Output:

subject[ 0] = Math
subject[ 1] = Language
subject[ 2] = History
subject[ 3] = English

Pictorial Presentation:

php function reference: rsort() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: reset
Next: shuffle



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

Filters the collection using the given callback

Example:

<?php
function tips_reject($items, $func)
{
  return array_values(array_diff($items, array_filter($items, $func)));
}

print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) {
  return strlen($item) > 4;
}));
?>

Output:

Array
(
    [0] => Pear
    [1] => Kiwi
)