PHP array in reverse order

Source: Internet
Author: User
In an article "How to sort PHP arrays", we introduced sort, asort, and ksort, which sort arrays in ascending order, so what if we want to implement the inverted order of the array? The following is another group of functions we want to talk about: rsort, arsort, and krsort. we will introduce these functions one by one! PHP array in reverse order

In an article "How to sort PHP arrays", we introduced sort, asort, and ksort, which sort arrays in ascending order, so what if we want to implement the inverted order of the array? The following is another group of functions we want to talk about: rsort, arsort, and krsort. we will introduce these functions one by one!

Rsort

Rsort-sort the values in descending order.

The rsort () function sorts array elements in reverse order by key value. Similar to arsort.

The syntax format is as follows:

bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

The following is an example of the rsort function. the specific code is as follows:

  "80", "joe" => "85", "tom" => "100","hank" => "60");rsort($arr);print_r($arr);?>

The output result is:

In the above example, I don't know what problems have you found?

Note: This function assigns a new key name to the unit in array. This will delete the original key name, not just the reorder.

Arsort

Arsort-sorts arrays in reverse order and maintains the index relationship.

The arsort () function sorts the array, and the index of the array remains associated with the unit. It is mainly used to sort the arrays that are very important to the unit order.

The syntax structure is as follows:

bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

The syntax, usage, and functions of the arsort and rsort functions are basically the same, but they are totally different! For details, see the following example:

  "80", "joe" => "85", "tom" => "100","hank" => "60");arsort($arr);print_r($arr);?>

The output result is:

Compared with the above rsort instance, I believe many people understand the biggest difference between the two functions!

Note: the difference between the arsort function and the rsort function is that the arsort () function sorts the associated arrays in descending order by key value, and the rsort function assigns a new key name to the unit in the array. This will delete the original key name, not just the reorder.

Krsort

Krsort-sort arrays 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 by key name, and retains the association between key names and data. It is mainly used to combine arrays.

The following is an example of the rsort function. the specific code is as follows:

  "80", "joe" => "85", "tom" => "100","hank" => "60");krsort($arr);print_r($arr);?>

Output result:

[Recommended tutorials]

1. related topic recommendations: php Array (Array)

2. related video course recommendations:

Sort arrays by values: sort () forward and rsort () reverse sorting functions

Sort by array key names: ksort () ascending and krsort () descending functions

Keep the key-value relationship unchanged during array sorting: asrot () and arsort () functions

The above is the detailed content of the PHP array in reverse order. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.