Rsort () of php array function sorting-sort element values in descending order

Source: Internet
Author: User
This article briefly introduces how php uses its own functions to sort the element values of arrays in descending order. For more information, see rsort () the function sorts the elements of the array in reverse order by key value, which is similar to the function of arsort ().

This article briefly explains how php uses its own functions to sort the element values of arrays in descending order. For more information, see.

The rsort () function sorts array elements in reverse order based on the key value. This function is basically the same as that of arsort.

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

TRUE is returned if the operation is successful. otherwise, FALSE is returned.

The second optional parameter contains another sorting flag.

Syntax

Rsort (array, sorttype) parameter description

Array required. input array.

Sorttype is optional. specifies how to arrange the values of the array. possible values;

SORT_REGULAR-default. process with their original type (without changing the type ).

SORT_NUMERIC-process the value as a number

SORT_STRING-process the value as a string

SORT_LOCALE_STRING-process the value as a string based on local settings *.

*; This value is newly added to PHP 4.4.0 and 5.0.2. before PHP 6, you can use setlocale () to change the region settings of the system. from PHP 6, the i18n_loc_set_default () function must be used.

The instance code is as follows:

  1. $ My_array = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
  2. Rsort ($ my_array );
  3. Print_r ($ my_array );
  4. ?> Output;
  5. Array
  6. (
  7. [0] => Horse
  8. [1] => Dog
  9. [2] => Cat
  10. )
  11. Like sort (), rsort () assigns new keys for the elements in array. it will remove any existing keys you may have assigned, rather than just reordering the keys. this means that it will destroy associative keys.
  12. $ Animals = array ("dog" => "large", "cat" => "medium", "mouse" => "small ");
  13. Print_r ($ animals );
  14. // Array ([dog] => large [cat] => medium [mouse] => small)
  15. Rsort ($ animals );
  16. Print_r ($ animals );
  17. // Array ([0] => small [1] => medium [2] => large)
  18. Use KSORT () or KRSORT () to preserve associative keys.

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.