rsort() function in PHP || PHP Rsort Function
×


rsort() function in php

1666

PHP rsort() function is used to sort a PHP indexed array in descending order. It is an inbuilt function of PHP.

NOTE: For ascending order use sort() PHP function.

Syntax:

rsort($array, $sorting_type);

here,

$array is a PHP array. it is mandatory.

$sorting_type is a numeric value that decides the type of comparing of array elements. It is optional. It has 6 possible values that specify 6 possible sorting orders.

  • SORT_REGULAR: if a value is 0, then the function compares array elements normally.
  • SORT_NUMERIC: if a value is 1, then the function compares array elements as a numeric value.
  • SORT_STRING: if a value is 2, then the function compares array elements as a string.
  • SORT_LOCALE_STRING: if a value is 3, then this function compares array elements as a string according to the current locale.
  • SORT_NATURAL: if a value is 4, then the function compares array elements as a string with the use of natural ordering.
  • SORT_FLAG_CASE: if a value is 5, then this function compares array elements according to value.

Example:

<?php
$arr1 = array("pineapple","guava","pear","apple","orange","grapes","banana");
echo "<pre>";
echo "unsorted array : <br>";
print_r($arr1);
rsort($arr1);
echo "<br> sorted array : <br>";
print_r($arr1);
?>

Output:




Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments