Home  >  Article  >  Backend Development  >  PHP array sorting in reverse order

PHP array sorting in reverse order

黄舟
黄舟Original
2017-05-04 11:34:1428880browse

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:

PHP array sorting in reverse order

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 ] )

The syntax, usage and functions of the arsort function and the rsort function are basically the same, but they have one thing that is completely different! Specifically, let’s look at the following example:

<?php
header("Content-Type:text/html; charset=utf-8");
$arr=array("andy" => "80", "joe" => "85", "tom" => "100","hank" => "60");
arsort($arr);
print_r($arr);
?>

The output result is:

PHP array sorting in reverse order

Compared with the rsort example above, I believe many people understand the difference between these two functions. What’s the biggest difference!

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:

PHP array sorting in reverse orderThis 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to sort PHP array?Next article:How to sort PHP array?