Home >Backend Development >PHP Tutorial >php array sorting function
This article introduces 6 PHP array sorting functions, I hope it will be helpful to you.
sort() – Sort the array in ascending order (passing a reference will modify the original array)
rsort() – Sort the array in descending order Arrange (passing by reference will modify the original array)
asort() – Sort the array in ascending order based on the value of the associated array
ksort () – Sort the array in ascending order based on the keys of the associative array
arsort() – Sort the array in descending order based on the values of the associative array
krsort() – Sort the array in descending order according to the key of the associated array
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/3/12 * Time: 9:46 */ $arr = ['Tom'=>'11','Jerry'=>20,'heros'=>['caption','superman']]; print_r($arr); sort($arr); print_r($arr); rsort($arr); print_r($arr); asort($arr); print_r($arr); ksort($arr); print_r($arr); arsort($arr); print_r($arr); krsort($arr); print_r($arr);
The execution results are as follows
Array ( [Tom] => 11 [Jerry] => 20 [heros] => Array ( [0] => caption [1] => superman ) ) Array ( [0] => 11 [1] => 20 [2] => Array ( [0] => caption [1] => superman ) ) Array ( [0] => Array ( [0] => caption [1] => superman ) [1] => 20 [2] => 11 ) Array ( [2] => 11 [1] => 20 [0] => Array ( [0] => caption [1] => superman ) ) Array ( [0] => Array ( [0] => caption [1] => superman ) [1] => 20 [2] => 11 )
Things to note The processing of arrays in PHP is all reference processing, which means that the original array will be modified. Therefore, if you do not want to modify the original array, it is recommended to create a new array machine for sorting operations.
Of course, generally when we want to sort, we still want the original array to be modified into a sorted array.
For more PHP related knowledge, please visit PHP Chinese website !
The above is the detailed content of php array sorting function. For more information, please follow other related articles on the PHP Chinese website!