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:

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 = '';
$to = '';
$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 : //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]

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

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

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

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

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

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

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



Chủ Đề