PHP array sort function array_multisort () function

Source: Internet
Author: User
Tags array sort

This article describes the contents of the PHP array sorting function Array_multisort () function in detail, has a certain reference value, now share to everyone, the need for friends can refer to

Array_multisort () This function can sort multiple PHP arrays, and the result is that all arrays are arranged in the order of the first array--a bit awkward, really, and I'm going to just say that you certainly don't understand. Let me give you an example:

For example Array_multisort ($a, $b), $a, $b is two arrays, and if after sorting, the 3rd element of the $a array is ranked first, then $b's third element, regardless of his size in $b, will be ranked first. Look at the results of the program running below: $a =array (100,80,50,10,0), $b = Array ("C", "F", "Q", "E", "Z"), Array_multisort ($a, $b); Var_dump ($a); var_ Dump ($b); Run Result: Array (5) {[0]=> int (0) [1]=> Int (ten) [2]=> int ([3]=> int) [4]=> int (+)}array (5) { [0]=> string (1) "Z" [1]=> string (1) "E" [2]=> string (1) "Q" [3]=> string (1) "F" [4]=> string (1) "C"} actually, that's clear. That is, array_multisort () sorts the first array by the size of the key value, and then the other arrays are adjusted according to the adjustment strategy of the first array-the third element is placed first, the second element is placed in the second position ...-in fact, the most basic embodiment of this multi-dimensional array sorting algorithm!
    • However, it is important to note that the number of elements in the two array must be the same, or a warning message will appear

First, let's look at how to sort each element of a multidimensional array [array], which is simple, but there are a few parameters that need to be explained, and if you have an idea of SQL, you'll see:

Let's construct a multidimensional array $a=array (100,2,4,7,7), $b =array (' ab ', ' AC ', ' ad ', ' AG ', ' ap '), $ab = Array ($a, $b);//Start Sorting Array_multisort ( $ab [0],sort_numeric,sort_desc, $ab [1],SORT_STRING,SORT_ASC];p rint_r ($AB); Explain: first we use Sort_numeric to declare to $ab[0] Sort by number type, with SORT_DESC declaration order is reversed (from large to small), and then we sort $ab[1] by string type, order is ascending (order) The last array $ab The result is the combination of the two, first press $ab[0] reverse, if $ab[0] The values in the same size are arranged in the order of $ab[1], and the output is as follows: Array ([    0] = = Array ([0] = [1] = + 7 [2] = 7 [3] = 4 [4] +/-[+] 2)    [1] = = Array ([0] = AB [1] = + AG [2] = + ap [3] = = AD [4] = + AC))

Now let's look at an example that is closer to the actual application:

$array [] = Array ("Age" =>20, "name" + "Li"), $array [] = Array ("Age" =>21, "name" + "AI"), $array [] = Array ("Age" =>20, "name" = "CI"), $array [] = Array ("Age" =>22, "name" = "Di"), foreach ($array as $key = = $value) {     $ age[$key] = $value [' age '];     $name [$key] = $value [' name '];} Array_multisort ($age, Sort_numeric,sort_desc, $name, SORT_STRING,SORT_ASC, $array);p Rint_r ($array);

The $array[] array of this example is constructed according to the records that are read in the database, and we now rank them in the order of age from large to small, in the order of their names, if they are the same age. That sort of thing is what we'll use in the future.

Because Array_multisort () needs a sort parameter that must be a column, we use foreach to read the age and name of the array, and then what?

Just like the example above, the last parameter $array must be seen, yes, it is necessary to declare which array to sort, because our front two parameters in the form already and need to sort the PHP array has no relationship, although they are actually the data in the $array-we The columns extracted in the $array--sort of course are required columns, have not yet seen using row data to sort it!

The output is as follows--as we thought:

Array ([    0] = = Array ([age] = [name] + di)    [1] = = Array ([age] = [name] = + ai)    [2] = = Array ([age] = [name] + ci)    [3] = = Array ([age] = [name] = + li))

See, in fact, is also very simple, is that the number of parameters need to capitalize a little annoying! Although it is a bit difficult to understand, but understand the good, the future is very useful oh!

Array_multisort (array1,sorting order,sorting type,array2,array3 ...)

    • Sorting order is optional. Specify the order of arrangement. Possible values:

      SORT_ASC-Default. Sorted in ascending order (A-Z).
      Sort_desc-Sorted in descending order (Z-A).

    • Sorting type is optional. Specifies the sort type. Possible values:

      Sort_regular-Default. Arrange each item in the usual order (standard ASCII, without changing the type).
      Sort_numeric-Handle each item as a number.
      Sort_string-handles each item as a string.
      Sort_locale_string-handles each item as a string, based on the current locale (can be changed by setlocale ()).
      Sort_natural-handles each item as a string, using a natural sort similar to Natsort ().
      Sort_flag_case-You can sort strings by combining (bitwise OR) sort_string or sort_natural, not case-sensitive.

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.