Home  >  Article  >  Backend Development  >  Six ways to sort arrays in PHP

Six ways to sort arrays in PHP

墨辰丷
墨辰丷Original
2018-05-16 14:57:491633browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn