PHP array sort

PHP Array Sorting


The elements in the array can be arranged in descending or ascending order alphabetically or numerically.


PHP - Array sorting function

In this chapter, we will introduce the following PHP arrays one by one Sorting function:

· sort() - Sort the array in ascending order

· rsort() - Sort the array in descending order

· asort() - Sort the array according to the Value, sort the array in ascending order

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

·     arsort() - Sort the array in descending order based on the value of the associative array Arrangement

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

·         shuffle() -  Randomly sort the elements in the array

· array_reverse() - Reverse is to reverse the order of each element in an original array



##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() - Sort the array in descending order

The following example sorts the elements in the $cars array 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 based on its value 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

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



##arsort() - Sort the array in descending order based on 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 is based on the key of the array , sort the associative array in descending order: Instance

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

shuffle() - Randomly sort the elements in the array

Use the shuffle function to randomly sort the elements in the array bool shuffle ( array &$array )

<?php
$my_array = array("red","green","blue","yellow","purple");shuffle($my_array);
print_r($my_array);
?>

Every time the above code is executed, the order of output is different. We use it to achieve random ranking

Note: After the associative array is shuffled, Keys will be lost


array_reverse() - Reverse is to reverse the order of each element in an original array

Reverse order ≠ Descending order

Reverse order is to reverse the order of each element in an original array

array array_reverse ( array $array [, bool $preserve_keys = false ] )

If the array is an associative array

• The reverse order will not be affected when the keys are characters, and the keys 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 numerical key will be retained in reverse order

<?php
$names = array(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!


Continuing Learning
||
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); arsort($age); ?>
submitReset Code