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:
Example <?php
$cars =array ("Volvo", "BMW", "Toyota");
Sort ($cars);
?>
The following instance arranges the elements in the $numbers array in ascending order of numbers:
Example <?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:
Example <?php
$cars =array ("Volvo", "BMW", "Toyota");
Rsort ($cars);
?>
The following instance arranges the elements in the $numbers array in descending order of numbers:
Example <?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:
Example <?php
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
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:
Example <?php
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
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:
Example <?php
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
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:
Example <?php
$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");
Krsort ($age);
?>
PHP array sorting