PHP array sorting
The elements in the array can be arranged in descending or ascending order alphabetically or numerically.
PHP - Array sorting functions
In this chapter, we will introduce the following PHP array sorting functions one by one:
sort() - Sort an array in ascending order
rsort() - Sort an array in descending order
asort() - Sort an array in ascending order based on the values of an associative array
ksort() - Sort an 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 based on the values of the associative array keys, sort the array in descending order
sort() - sort the array in ascending order
The following example sorts the $cars array The elements are sorted in ascending alphabetical order:
Example
<?php $cars=array("Volvo","BMW","Toyota"); sort($cars); print_r($cars); ?>
Try it out»
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); print_r($numbers); ?>
Try it »
rsort() - Sort the array in descending order
The following example will $cars The elements in the array are sorted in descending alphabetical order:
Example
<?php $cars=array("Volvo","BMW","Toyota"); rsort($cars); print_r($cars); ?>
Try it»
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); print_r($numbers); ?>
Try it»
asort() - Sort the array in ascending order according to the value of the array
Below The example of 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); print_r($age); ?>
Try it »
ksort() - According to the value of the array Key, sort the array in ascending order
The following example sorts the associative array in ascending order according to the key of the array:
Example
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); ksort($age); print_r($age); ?>
Try it»
arsort() - Sort the array in descending order according to the value of the array
The following Example Sort an associative array in descending order based on the value of the array:
Example
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); arsort($age); print_r($age); ?>
Try it »
krsort() - based on the key of the array , sort the array in descending order
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); print_r($age); ?>
Try it »
There are also 2 functions to explain:
1.shuffle random shuffle
The shuffle function can randomly sort the elements in the array
Example
<?php $numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; shuffle($number); print_r($number); ?>
Every time the above code is executed, the output order is different. Note: Associative array After being shuffled, the keys will be lost
2. array_reverse reverse order function
The reverse order is to The order of each element in an original array is flipped to reverse order ≠ descending order
If the array is an associative array:
The reverse order is not affected when the key is a character, and the key will still be retained
When the key is a number, the default key will be reset to 0, 1, 2 after reverse order...
When the second parameter is true, the key is a number, and the reverse order will retain the numeric key
Example
<?php $names = [10 => '张三', 60 => '阿毛', 30 => '李四', 25 => '宝哥']; print_r(array_reverse($names)); print_r(array_reverse($names, true)); ?>
Complete PHP Array Reference Manual
For a complete reference manual for all array functions, please visit our PHP Array Reference Manual.
This reference manual provides a brief description and application examples of each function!