This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.
In this chapter, we will go through the following PHP array sort functions:
The following example sorts the elements of the $cars array in ascending alphabetical order:
<?php $cars = array("Volvo", "BMW", "Toyota"); sort($cars); ?>
The following example sorts the elements of the $numbers array in ascending numerical order:
<?php $numbers = array(4, 6, 2, 22, 11); sort($numbers); ?>
The following example sorts the elements of the $cars array in descending alphabetical order:
<?php $cars = array("Volvo", "BMW", "Toyota"); rsort($cars); ?>
The following example sorts the elements of the $numbers array in descending numerical order:
<?php $numbers = array(4, 6, 2, 22, 11); rsort($numbers); ?>
The following example sorts an associative array in ascending order, according to the value:
<?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); asort($age); ?>
The following example sorts an associative array in ascending order, according to the key:
<?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); ksort($age); ?>
The following example sorts an associative array in descending order, according to the value:
<?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); arsort($age); ?>
The following example sorts an associative array in descending order, according to the key:
<?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); krsort($age); ?>
Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
Animated PHP icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Module 4 quiz