Array sort
PHP - Array sorting function
In this chapter, we will introduce the following PHP array sorting functions one by one:
sort() - Sort the array in ascending order
rsort() - Sort the array in descending order
asort() - Sort the array according to the value of the associative array Sort the array in ascending order
ksort() - Sort the array in ascending order according to the keys of the associative array
arsort() - Sort the array according to the keys of the associative array Sort the array in descending order
krsort() - Sort the array in descending order according to the key of the associated array
sort() - Sort the array in ascending order
The following example sorts the elements in the $cars array in ascending alphabetical order:
Example
<?php $cars=array("Volvo","BMW","Toyota"); sort($cars); ?>
The following example sorts the elements in the $numbers array in ascending numerical order:
Example
<?php $numbers=array(4,6,2,22,11); sort($numbers); ?>
rsort() - Sort the array in descending order
The following example sorts the elements in the $cars array in descending alphabetical order:
Example
<?php $cars=array("Volvo","BMW","Toyota"); rsort($cars); ?>
The following example sorts the elements in the $numbers array in descending numerical order:
Example
<?php $numbers=array(4,6,2,22,11); rsort($numbers); ?>
asort() - Sort the array in ascending order according to the value of the array
The following example sorts the association according to the value of the array Sort the array in ascending order:
Example
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); asort($age); ?>
ksort() - Sort the array in ascending order according to the key of the array
The following example is based on the array The key, sort the associative array in ascending order:
Example
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); ksort($age); ?>
arsort() - Sort the array in descending order according to the value of the array
The following example sorts an associative array in descending order based on the array's value:
Example
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); arsort($age); ?>
krsort() - Sorts the array in descending order based on the array's key
Below Example of sorts an associative array in descending order according to the key of the array:
Example
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); krsort($age); ?>