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

Six ways to sort arrays in PHP

墨辰丷
Release: 2023-03-26 06:00:02
Original
1671 people have browsed it

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

2. rsort() sorts the array in descending order

$string=array("M","A","C");
sort($string);
print_r($string);123
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);123
Copy after login

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

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

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

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!

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