There are 12 types of php array sorting: 1. Use sort() to sort the array in ascending order; 2. Use rsort() to sort the array in descending order; 3. Use asort() to sort the array in ascending order based on the value of the associated array. Arrange; 4. Use ksort() to sort in ascending order according to the keys of the associative array; 5. Use krsort() to sort in descending order, etc.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In the process of using PHP arrays, we have Sometimes you need to sort a PHP array.
In PHP, the elements in an array can be arranged in descending or ascending order alphabetically or numerically. The following is PHP's built-in array sorting function:
Description | |
---|---|
Sort multiple arrays or multidimensional arrays. | |
Sort the associative array in descending order by key value. | |
Sort the associative array in ascending order by key value. | |
Sort the associative array in descending order by key name. | |
Sort the associative array in ascending order by key name. | |
Use the "natural sorting" algorithm to sort the array in a case-insensitive manner. | |
Sort the array using the "natural sorting" algorithm. | |
Sort the numeric array in descending order. | |
Sort the numeric array in ascending order. | |
Use a user-defined comparison function to sort the key values in the array. | |
Use a user-defined comparison function to sort the key names in the array. | |
Sort the array using a user-defined comparison function. |
#sort() - Sort the array in ascending order
The following example sorts the elements in the $cars array in ascending alphabetical order:<?php $cars=array("Volvo","BMW","Toyota"); var_dump($cars); sort($cars); var_dump($cars); ?>
<?php $numbers=array(4,6,2,22,11); var_dump($numbers); sort($numbers); var_dump($numbers); ?>
rsort() - Sort the array in descending order
The following example sorts the elements in the $cars array according to Alphabetical descending order:<?php $cars=array("Volvo","BMW","Toyota"); var_dump($cars); rsort($cars); var_dump($cars); ?>
<?php $numbers=array(4,6,2,22,11); var_dump($numbers); rsort($numbers); var_dump($numbers); ?>
asort() - Sort the array in ascending order based on the value of the array
The following example sorts the associative array in ascending order based on the value of the array:<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); var_dump($age); asort($age); var_dump($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:<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); var_dump($age); ksort($age); var_dump($age); ?>
arsort() - Sort the array in descending order according to the value of the array
The following example sorts the array according to the value of the array Sort the associative array in descending order:<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); var_dump($age); arsort($age); var_dump($age); ?>
krsort() - Sort the array in descending order according to the key of the array
The following The example sorts the associated array in descending order according to the key of the array:<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); var_dump($age); krsort($age); var_dump($age); ?>
PHP Video Tutorial"
The above is the detailed content of How many types of php array sorting are there?. For more information, please follow other related articles on the PHP Chinese website!