Home  >  Article  >  Backend Development  >  Six sorts of PHP arrays

Six sorts of PHP arrays

墨辰丷
墨辰丷Original
2018-05-10 14:24:119581browse

This article mainly introduces six sorts of PHP arrays. The elements in the array can be sorted in ascending or descending order in alphabetical or numerical order. Friends who need it can refer to it.

1. sort() sorts the array in ascending order

$string=array("M","B","A");
sort($string);
print_r($string);

2. rsort() sorts the array in descending order

$string=array("M","A","C");
sort($string);
print_r($string);

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);

##4. ksort() - Sort the array in ascending order according to the keys of the array

$person_age=array("john"=>"28","piter"=>"25","davies"=>"30");
ksort($person_age);
print_r($person_age);

5. arsort() Sort the array in descending order according to the values ​​of the array

$person_age=array("john"=>"28","piter"=>"25","davies"=>"30");
arsort($person_age);
print_r($person_age);

6. krsort() sorts the array in descending order according to the keys of the array

$person_age=array("john"=>"28","piter"=>"25","davies"=>"30");
krsort($person_age);
print_r($person_age);


Related recommendations:

PHP array sort function array_multisort() function detailed explanation

php array sort usort uksort sort function

Interpretation of PHP array sorting

Summary of PHP array sorting method (collection)



The above is the detailed content of Six sorts of PHP arrays. 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