About the application of knowledge related to PHP array sorting

jacklove
Release: 2023-03-25 13:46:01
Original
1381 people have browsed it

PHP Array sorting plays an important role in PHP. This article provides a detailed explanation of it.

PHP - Array sorting function

In this chapter, we will introduce the following PHP array sorting functions one by one:

sort() - Sort the array in ascending order

rsort() - Sort the array in descending order

asort() - Sort the array in ascending order based on the values ​​of the associative array

ksort() - Sort the array in ascending order based on the keys of the associative array Sort the array in ascending order

arsort() - Sort the array in descending order based on the values ​​of the associative array

krsort() - Sort the array in descending order based on the keys of the associative array Descending order

sort() - Sort the array in ascending order

The following example sorts the elements in the $cars array in ascending alphabetical order:

Example

<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);
?>
Copy after login

The following example sorts the elements in the $numbers array in ascending numerical order:

Example

<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
?>
Copy after login

rsort() - Sorts the array in descending order

The following example will The elements in the $cars array are sorted in descending alphabetical order:

Example

<?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
?>
Copy after login

The following example sorts the elements in the $numbers array in descending numerical order:

Example

<?php
$numbers=array(4,6,2,22,11);
rsort($numbers);
?>
Copy after login

asort() - Sort the array in ascending order according to the value of the array

The following example sorts the associative array in ascending order according to the value of the array:

Example

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);
?>
Copy after login

ksort() - Sort the array in ascending order according to the key of the array

The following example sorts the associative array in ascending order according to the key of the array:

Example

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($ age);
?>

arsort() - Sort the array in descending order according to the value of the array

The following example sorts the associative array in descending order according to the value of the array:

Example

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);
?>
Copy after login

krsort() - Sort the array in descending order according to the key of the array

The following example sorts the associative array in descending order according to the key of the array:

Example

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);
?>
Copy after login

This article provides a detailed explanation of php array sorting. For more learning materials, please pay attention to the php Chinese website.

Related recommendations:

Related knowledge and application of PHP file upload

PHP loop - understanding and use of While loop

Related knowledge and application of PHP 5 echo and print statements

The above is the detailed content of About the application of knowledge related to PHP array sorting. 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