PHP Array function sort rsort ()-Descending order of element values of the arrays _php tutorial

Source: Internet
Author: User
This article briefly explains how PHP uses its own function array of element values in descending order method, there is a need for reference.


The Rsort () function reverses the order of the elements of the array by key values. The function is basically the same as Arsort ().

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

Returns TRUE if successful, otherwise FALSE.

The Second optional parameter contains a different sort flag.

Grammar
Rsort (array,sorttype) parameter description
Array required. The input array.
SortType is optional. Specifies how the values of the array are arranged. Possible values:

Sort_regular-Default. Processed in their original type (without changing the type).
Sort_numeric-Handle the value as a number
Sort_string-handles the value as a string
Sort_locale_string-handles the value as a string, based on local settings *.

*: This value is new for PHP 4.4.0 and 5.0.2. Prior to PHP 6, the system's locale was used, which can be changed using setlocale (). From PHP 6, you must use the I18n_loc_set_default () function.

Example

The code is as follows Copy Code

$my _array = Array ("A" = "Dog", "b" = "Cat", "c" = "Horse");
Rsort ($my _array);
Print_r ($my _array);
?> output:

Array
(
[0] = Horse
[1] = Dog
[2] = Cat
)
Like sort (), Rsort () assigns new keys for the elements in array. It'll remove any existing keys and assigned, rather than just reordering the keys. This means, it'll destroy associative keys.

$animals = Array ("dog" = "large", "cat" = "medium", "mouse" = "small");
Print_r ($animals);
Array ([dog] = large [cat] + medium [mouse] = small)

Rsort ($animals);
Print_r ($animals);
Array ([0] = small [1] = medium [2] = Large)

Use Ksort () or krsort () to preserve associative keys.

http://www.bkjia.com/PHPjc/629259.html www.bkjia.com true http://www.bkjia.com/PHPjc/629259.html techarticle This article briefly explains how PHP uses its own function array of element values in descending order method, there is a need for reference. The Rsort () function sets the elements of an array according to the key values ...

  • Related Article

    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.