Write a php script to sort an array in reverse order highest to lowest

Last update on August 19 2022 21:50:29 (UTC/GMT +8 hours)

PHP Array: Exercise-28 with Solution

Write a PHP script to sort an array in reverse order (highest to lowest).

Sample Solution:

PHP Code:



Sample Output:

Array                                                       
(                                                           
    [0] => White                                            
    [1] => Red                                              
    [2] => Orange                                           
    [3] => Black                                            
)

Flowchart:

Write a php script to sort an array in reverse order highest to lowest

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP function to generate a random password (contains uppercase, lowercase, numeric and other) using shuffle() function.
Next: Write a PHP program to generate an array with a range taken from a string.

PHP: Tips of the Day

PHP: Send email using the GMail SMTP server from a PHP page.

// Pear Mail Library
require_once "Mail.php";

$from = '<[email protected]>';
$to = '<[email protected]>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('

' . $mail->getMessage() . '

'); } else { echo('

Message successfully sent!

'); }

Ref : https://bit.ly/3aE6MFt

Introduction

PHP is a server scripting language, and It is a powerful tool for making interactive and dynamic Web-pages. I have used WampServer 2.2 for following excercise..

Result

Write a php script to sort an array in reverse order highest to lowest
Write a PHP script to sort an array in reverse order (highest to lowest)


The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.


PHP - Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:

  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key

Sort Array in Ascending Order - sort()

The following example sorts the elements of the $cars array in ascending alphabetical order:

Example

$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>

Try it Yourself »

The following example sorts the elements of the $numbers array in ascending numerical order:



Sort Array in Descending Order - rsort()

The following example sorts the elements of the $cars array in descending alphabetical order:

Example

$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>

Try it Yourself »

The following example sorts the elements of the $numbers array in descending numerical order:


Sort Array (Ascending Order), According to Value - asort()

The following example sorts an associative array in ascending order, according to the value:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>

Try it Yourself »


Sort Array (Ascending Order), According to Key - ksort()

The following example sorts an associative array in ascending order, according to the key:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>

Try it Yourself »


Sort Array (Descending Order), According to Value - arsort()

The following example sorts an associative array in descending order, according to the value:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>

Try it Yourself »


Sort Array (Descending Order), According to Key - krsort()

The following example sorts an associative array in descending order, according to the key:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>

Try it Yourself »


Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.

The reference contains a brief description, and examples of use, for each function!


PHP Exercises