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

Source: Internet
Author: User

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

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

If the call succeeds, TRUE is returned. Otherwise, FALSE is returned.

The second optional parameter contains another sorting flag.

Rsort (array, sorttype) parameter description
Array is required. Input array.
Sorttype is optional. Specifies how to arrange the values of an 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, you must use the i18n_loc_set_default () function.


The code is as follows: Copy code

<? Php
$ My_array = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Rsort ($ my_array );
Print_r ($ my_array );
?> Output:

[0] => Horse
[1] => Dog
[2] => Cat
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.

$ 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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.