Jump to: navigation, search

Php rsort

From w3cyberlearnings

Contents

PHP function rsort

This function sorts array by values in reverse order.

Syntax rsort

  • array: array input
  • sorttype (optional):
    • SORT_REGULAR - (default, don't change type and treat value as it is),
    • SORT_NUMERIC - Treat value as numeric
    • SORT_STRING - Treat value as string
    • SORT_LOCAL_STRING - Treat value based on the local setting.
rsort(array,sorttype);

Note

The rsort function uses quicksort algorithm to reverse sort. Look at sort() function.

Example 1

<?php
$score = array(30,20,50,3,5,110,10);

print_r($score);
rsort($score);
print_r($score);
?>

Output

Array
(
    [0] => 30
    [1] => 20
    [2] => 50
    [3] => 3
    [4] => 5
    [5] => 110
    [6] => 10
)
Array
(
    [0] => 110
    [1] => 50
    [2] => 30
    [3] => 20
    [4] => 10
    [5] => 5
    [6] => 3
)

Example 2: rsort by string

<?php
$score = array(30,20,50,3,5,110,10);

print_r($score);
rsort($score,SORT_STRING);
print_r($score);
?>

Output

Array
(
    [0] => 30
    [1] => 20
    [2] => 50
    [3] => 3
    [4] => 5
    [5] => 110
    [6] => 10
)
Array
(
    [0] => 50
    [1] => 5
    [2] => 30
    [3] => 3
    [4] => 20
    [5] => 110
    [6] => 10
)

Related Links


Navigation
Web
SQL
MISC
References