Write a php script to sort an array in reverse order highest to lowest
PHP Array: Exercise-28 with SolutionWrite a PHP script to sort an array in reverse order (highest to lowest). Show
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. PHP: Tips of the DayPHP: 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(' Ref : https://bit.ly/3aE6MFt IntroductionPHP 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.. ResultWrite 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 ArraysIn this chapter, we will go through the following PHP array sort functions:
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"); 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"); 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"); 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"); 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"); 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"); Try it Yourself » Complete PHP Array ReferenceFor 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 |