Six sorts of PHP arrays

墨辰丷
Release: 2023-03-25 18:52:02
Original
9572 people have browsed it

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);
Copy after login

2. rsort() sorts the array in descending order

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

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);
Copy after login

##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);
Copy after login

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);
Copy after login

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);
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!