Tuesday 6 April 2021

Php: rsort: Sort array contents in descending order

‘rsort’ function return the elements in descending order (in place, affect the original array).

 

Syntax

rsort($array_name);

 

Example

rsort($arr);

 

Find the below working example.

 

array_reverse_sort_demo.php

#!/usr/bin/php

<?php

    $arr = array(22, 31, -5, 77, -98, 18);
    
    echo "Original array : \n";
    print_r($arr);

    rsort($arr);

    echo "Sorted array in descending order : \n";
    print_r($arr);
    
?>

 

Output

$./array_reverse_sort_demo.php 

Original array : 
Array
(
    [0] => 22
    [1] => 31
    [2] => -5
    [3] => 77
    [4] => -98
    [5] => 18
)
Sorted array in descending order : 
Array
(
    [0] => 77
    [1] => 31
    [2] => 22
    [3] => 18
    [4] => -5
    [5] => -98
)

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment