Sort elements in an array in descending order PHP program

PHP Functions For Sorting Array In Descending Order: rsort().
The rsort() function sorts an indexed array in descending order.

Sort the elements of the $MyArray array in descending numerical order:

<?php
    $MyArray=array(8, 20, 4, 100, 50, 6, 9, 11, 22, 5);
    rsort($MyArray);
    print_r($MyArray);
?>

Output:

Array ( [0] => 100 [1] => 50 [2] => 22 [3] => 20 [4] => 11 [5] => 9 [6] => 8 [7] => 6 [8] => 5 [9] => 4 ) 

Sort the elements of the $MyArray array in descending alphabetical order:

<?php
    $MyArray=array("PHP", "HTML", "CSS", "JavaScript", "Mysql", "Java", "AngularJs", "WordPress");
    rsort($MyArray);
    print_r($MyArray);
?>

Output:

Array ( [0] => WordPress [1] => PHP [2] => Mysql [3] => JavaScript [4] => Java [5] => HTML [6] => CSS [7] => AngularJs ) 

Sorting using krsort() – Sort an array by key in reverse order

<?php
    $MyArray=array("PHP", "HTML", "CSS", "JavaScript", "Mysql", "Java", "AngularJs", "WordPress");
    krsort($MyArray);
    print_r($MyArray);
?>

Output:

Array ( [7] => WordPress [6] => AngularJs [5] => Java [4] => Mysql [3] => JavaScript [2] => CSS [1] => HTML [0] => PHP ) 

Leave a Reply

Related Posts: You may like

Find Armstrong Number

Write a program find Factorial of a number

Write a PHP program to Print table of Number

Write a PHP program to print prime number

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.