Sort the element values of the php array in descending order by rsort () function

Source: Internet
Author: User
Sort the array values. in addition to the various sorting methods taught by the instructor, our php will directly give you a function rsort (), which is very simple to use, but only suitable for arrays. Rsort (array

Sort the array values. in addition to the various sorting methods taught by the instructor, our php will directly give you a function rsort (), which is very simple to use, but only suitable for arrays.

Rsort (array, sorttype)

Parameter description

Array is required. Input array.

Sorttype (optional) specifies how to sort the values of an array. possible values:

SORT_REGULAR-by default, it is processed based on 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.

The code is as follows:

  1. $ My_array = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
  2. Rsort ($ my_array );
  3. Print_r ($ my_array );
  4. ?>
  5. // Output:
  6. Array
  7. (
  8. [0] => Horse
  9. [1] => Dog
  10. [2] => Cat
  11. )

For other references, the code is as follows:

  1. $ Data [] = array ('Volume '=> 67, 'version' => 2 );
  2. $ Data [] = array ('Volume '=> 86, 'version' => 1 );
  3. $ Data [] = array ('Volume '=> 85, 'version' => 6 );
  4. $ Data [] = array ('Volume '=> 98, 'version' => 2 );
  5. $ Data [] = array ('Volume '=> 86, 'version' => 6 );
  6. $ Data [] = array ('Volume '=> 67, 'version' => 7 );

In this example, we will sort volume in descending order and edition in ascending order. now we have an array containing rows, but array_multisort () needs an array containing columns, therefore, use the following code to retrieve and sort columns.

Get the list of columns, the code is as follows:

  1. Foreach ($ data as $ key => $ row ){
  2. $ Volume [$ key] = $ row ['Volume '];
  3. $ Edition [$ key] = $ row ['version'];
  4. }
  5. // Sort data by volume in descending order and edition in ascending order
  6. // Use $ data as the last parameter and sort it by a common key
  7. Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );

The data set is sorted as follows:

  1. Volume | edition
  2. ------- + --------
  3. 98 | 2
  4. 86 | 1
  5. 86 | 6
  6. 85 | 6
  7. 67 | 2
  8. 67 | 7

In fact, there are still many ways to use in sorting, such as the arsort (), asort (), ksort (), krsort (), natsort (), natcasesort (), rsort (), usort (), array_multisort (), and uksort ().

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.