Home >Backend Development >PHP Tutorial >Six ways to sort arrays in PHP
This article mainly introduces six ways of sorting arrays in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
Six sorting methods for arrays:
1. sort() sorts the array in ascending order
$string=array("M","B","A"); sort($string); print_r($string);123
2. rsort() sorts the array in descending order
$string=array("M","A","C"); sort($string); print_r($string);123
3. asort() sorts the array in ascending order according to the value of the array
$person_age=array("john"=>"28","piter"=>"25","davies"=>"30"); asort($person_age); print_r($person_age);123
4. ksort() - sorts the array in ascending order according to the key of the array
$person_age=array("john"=>"28","piter"=>"25","davies"=>"30"); ksort($person_age); print_r($person_age);123
5. arsort() sorts the array in descending order according to the value of the array
$person_age=array("john"=>"28","piter"=>"25","davies"=>"30"); arsort($person_age); print_r($person_age);123
6. krsort() sorts the array in descending order according to the key of the array
$person_age=array("john"=>"28","piter"=>"25","davies"=>"30"); krsort($person_age); print_r($person_age);
Related recommendations:
About PHP array sorting related knowledge using
PHP index array sorting method
php two-dimensional array sorting
The above is the detailed content of Six ways to sort arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!