PHP Array_multisort () function

Source: Internet
Author: User

This function because of use, and in the Internet to find a half-day finally found a write easy-to-read article, here to share to everyone.

Original link: http://blog.163.com/lgh_2002/blog/static/4401752620115242345435/

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:

1
2
3
4
5
6
7

$a =array (100,80,50,10,0);
$b = Array ("C", "F", "Q", "E", "Z");
Array_multisort ($a, $b);
Var_dump ($a);
Var_dump ($b);
?>

Operation Result:
Array (5) {[0]=> int (0) [1]=> Int (ten) [2]=> int ([3]=> int) [4]=> int (100)}
Array (5) {[0]=> string (1) "Z" [1]=> string (1) "E" [2]=> string (1) "Q" [3]=> string (1) "F" [4]=> string (1) "C"}

It is clear that the z of the fifth element of array B is ranked first!

In fact, Array_multisort () sort the first array by the size of the key values, 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, this multi-dimensional array sorting algorithm is the most basic embodiment!

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:
Warning:array_multisort () [Function.array-multisort]: Array sizes is inconsistent in ...

Well, I hope you can use the above, we still say the main bar: Array_multisort () to the multi-dimensional array to sort, this function will be very useful in the future to do the project!

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:

1
2
3
4
5
6
7
8
9
10
<?php
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];
Print_r ($AB);
?>

Note: First we use Sort_numeric to declare the $ab[0] by the number type, with the Sort_desc
The declaration order is reversed (from large to small), and then we sort the $ab[1] by string type, in ascending order (order)
The result of the final array $ab is the combination of the two, first press $ab[0] in reverse order, if the $ab[0] in the same size of the values are in accordance with $AB[1], 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)
)
Is it much like using order by in a database? Actually, it's almost there!

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14

$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);
Print_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. This sort is what we will 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!

PHP Array_multisort () function

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.