Home  >  Article  >  Backend Development  >  About the application of knowledge related to PHP array sorting

About the application of knowledge related to PHP array sorting

jacklove
jackloveOriginal
2018-05-07 11:47:541342browse

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

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

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

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

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

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

0305089d9ba82971af58decac0552028"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);
?>

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

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!

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