PhpArray Sorting
The elements in the array can be sorted in descending or ascending order, in alphabetical or numerical terms.
PHP-Array sorting function
In this chapter, we will introduce the following PHP array sorting functions:
- Sort ()-ascending order of arrays
- Rsort ()-Descending order of the array
- Asort ()-Sets the array in ascending order based on the values of the associative arrays
- Ksort ()-an array is sorted in ascending order based on the keys of the associative arrays
- Arsort ()-sorts arrays in descending order based on the values of the associative array
- Krsort ()-Sorts the arrays in descending order based on the keys of the associative array
Sort ()-ascending order of arrays
The following instance arranges the elements in the $cars array in ascending alphabetical order:
<? PHP $cars=array("Volvo", "BMW", "Toyota"); Sort ($cars);? >
The following instance arranges the elements in the $numbers array in ascending order of numbers:
<? PHP $numbers=array(4,6,2,22,11); Sort ($numbers);? >
Rsort ()-Descending order of the array
The following instance arranges the elements in the $cars array in descending alphabetical order:
<? PHP $cars=array("Volvo", "BMW", "Toyota"); Rsort ($cars);? >
The following instance arranges the elements in the $numbers array in descending order of numbers:
<? PHP $numbers=array(4,6,2,22,11); Rsort ($numbers);? >
Asort ()-Sorts the array in ascending order based on the values of the arrays
The following instance sorts the associative array in ascending order based on the value of the array:
<? PHP $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "" "); Asort ($age);? >
Ksort ()-Sorts the array in ascending order based on the key
The following instance sorts the associative array in ascending order, based on the key of the array:
<? PHP $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "" "); Ksort ($age);? >
Arsort ()-sorts arrays in descending order based on the values of the array
The following instance sorts the associative array in descending order, based on the value of the array:
<? PHP $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "" "); Arsort ($age);? >
Krsort ()-descending order of arrays based on the keys of the array
The following instance sorts the associative array in descending order, based on the key of the array:
<? PHP $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "" "); Krsort ($age);? >
PHP Quick Start Learning-11 (array sort)