Reverse order of PHP arrays
In an article "How to sort PHP arrays" we introduced sort and asort and ksort, they both sort the array in ascending order, so what if you want to implement the reverse order of the array? Here is another set of functions we are going to talk about: rsort, arsort, krsort Below we will introduce this set of functions one by one!
rsort
rsort — Sort a numeric array in descending order.
rsort() function sorts the elements of the array in reverse order by key value. Basically the same function as arsort().
The syntax format is as follows:
bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Let’s take an example to explain the rsort function in detail. The specific code is as follows:
<?php header("Content-Type:text/html; charset=utf-8"); $arr=array("andy" => "80", "joe" => "85", "tom" => "100","hank" => "60"); rsort($arr); print_r($arr); ?>
The output result is:
In the above example, did you find any problems?
Description: This function assigns a new key name to the unit in array. This will delete the original keys rather than just reorder them.
arsort
##arsort — Sort the array in reverse order and maintain the index relationship. The arsort() function sorts the array, and the index of the array remains associated with the unit. Mainly used for sorting associative arrays where the order of cells is important. The syntax structure is as follows:bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
<?php header("Content-Type:text/html; charset=utf-8"); $arr=array("andy" => "80", "joe" => "85", "tom" => "100","hank" => "60"); arsort($arr); print_r($arr); ?>
Explanation: The difference between the arsort function and the rsort function is that the arsort() function sorts the associative array in descending order by key value, while the rsort function assigns new key names to the cells in the array . This will delete the original keys rather than just reorder them.
krsort##krsort — Sort the array in reverse order by key name
The syntax structure is as follows:
bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
krsort sorts the array in reverse order according to the key name, retaining the association between the key name and the data. Mainly used for combining arrays.
Let’s take an example to explain the rsort function in detail. The specific code is as follows:
<?php header("Content-Type:text/html; charset=utf-8"); $arr=array("andy" => "80", "joe" => "85", "tom" => "100","hank" => "60"); krsort($arr); print_r($arr); ?>
The output result is:
This article introduces it in detail Three functions for array reversal, then we will give you a detailed introduction to the random reordering and reversing of arrays in the next article. For detailed introduction, please read "
Detailed Examples of PHP Array Random Reordering and Reverse Ordering
【Related tutorial recommendations】
1. Relevant topic recommendations: "
php array (Array) 》2. Recommended related video courses:
《
Sorting arrays by value: sort() forward and rsort() reverse sorting functions》《
Array sorting by key name: ksort() ascending order and krsort() descending order function》《
Keep the key-value correspondence unchanged when sorting the array: asrot() and arsort() functions》
The above is the detailed content of PHP array sorting in reverse order. For more information, please follow other related articles on the PHP Chinese website!