Array of PHP array element values are sorted in descending order Rsort () function _php Tutorial

Source: Internet
Author: User
Sorting array Values Besides I can use the various sorts of methods taught by the teacher, our PHP directly gives you a function rsort () function, the usage is very simple, but only suitable for the array ah.

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

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
)


Other references

The code is as follows Copy Code

$data [] = Array (' volume ' = +, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 1);
$data [] = Array (' volume ' = =, ' edition ' = 6);
$data [] = Array (' volume ' = = 98, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 6);
$data [] = Array (' volume ' = +, ' edition ' = 7);

In this example, the volume is sorted in descending order, and the edition in ascending order.

Now that you have an array with rows, but Array_multisort () requires an array of columns, so use the following code to get the columns and sort them.

Get a list of columns

The code is as follows Copy Code
foreach ($data as $key = = $row) {
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}

Sort data in descending order according to volume, in ascending order of edition
The $data as the last parameter, sorted by the Universal key
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);

The data set is now sorted and the results are as follows:

The code is as follows Copy Code

Volume | Edition
-------+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7

Actually there are many other ways to use it in the sort order, such as PHP's array of Arsort (), Asort (), Ksort (), Krsort (), Natsort (), Natcasesort (), Rsort (), Usort ( ), Array_multisort () and Uksort ().

http://www.bkjia.com/PHPjc/631308.html www.bkjia.com true http://www.bkjia.com/PHPjc/631308.html techarticle sorting array Values Besides I can use the various sorts of methods taught by the teacher, our PHP directly gives you a function rsort () function, the usage is very simple, but only suitable for the array ah. ...

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