PHP Multidimensional Array Sorting method

Source: Internet
Author: User
Tags foreach arrays sorted by name

Let me give you an example:

For example Array_multisort ($a, $b), $a, $b is two arrays, and if sorted, the 3rd element of the $a array is ranked first, then the third element of $b regardless of the size of his $b in the first place. Look at the following program running results:

The code is as follows Copy Code

<?php
$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 (x) [2]=> int (x) [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 was obvious that the z of the fifth element of array B was ranked first!

In fact, it's clear that array_multisort () first sorts the first array by the size of the key value, and then the other arrays are adjusted according to the adjustment policy of the first array-the third element is placed first, the second is placed in the second place ...- In fact, this multi-dimensional array sorting algorithm the most basic embodiment!

However, note that two arrays must have the same number of elements, or a warning message will appear:
Warning:array_multisort () [Function.array-multisort]: Array sizes are inconsistent in .....

Well, I hope we can use the above, we still say the main bar: Array_multisort () to the multidimensional array sorting, this feature in the future when the project is very useful!

First, let's look at how to sort each element of a multidimensional array, which is simple, but there are a few parameters to explain, and if you know something about SQL, you'll see it:

The code is as follows Copy Code
<?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);
?>

Explain: First we use Sort_numeric to declare to $ab[0] with the number type, with Sort_desc
The order of declarations is reverse (from big 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 by $ab[0], if the same number in $ab[0] in the order of the same size in $ab[1, the output results are as follows:

The code is as follows Copy Code
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 very much like using order by in the database? Actually, it's almost!

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

  code is as follows copy code

 < ? PHP
$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 read in the database, and we now arrange them in the order of age from big to small, if they are of the same age, sorted by name. This sort of arrangement is what we will often use in the future,
Because Array_multisort () needs a sort parameter that must be a column, so we use foreach to read the age and name of the array, after that?
Just like the example above, to sort, the last parameter $array presumably everyone has seen it, yes here you need to declare which array to sort on, because the two arguments we have in the first place have nothing to do with the PHP array that needs to be sorted, although they are actually the data in the $array--we The columns extracted in the $array--the sort of course requires columns, and has not yet been sorted with row data!

The output is as follows--as we think:

The code is as follows Copy Code
Array (
[0] => Array ([age] => [name] => di)
[1] => Array ([age] => [name] => ai)
[2] => Array ([age] => [name] => ci)
[3] => Array ([age] => [name] => Li)
)

See, in fact, it is also very simple, that is, those who need to capitalize the parameters a little annoying! Although also a bit difficult to understand, but understand the good, the future is very useful oh!
Appendix:
Sort order Flags:

sort_asc– Sorted by ascending order
sort_desc– Sorted in descending order

Sort Type Flags:

sort_regular– items are compared in the usual way
sort_numeric– the project by numerical comparison
sort_string– items by string comparison

You cannot specify two similar sort flags after each array. The sort flags specified after each array are valid only for the array-the default value SORT_ASC and Sort_regular before that.

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.