Home > Backend Development > PHP Tutorial > Learn to sort multidimensional arrays in php

Learn to sort multidimensional arrays in php

WBOY
Release: 2016-07-25 09:07:29
Original
1196 people have browsed it
  1. $a =array(100,80,50,10,0);
  2. $b = array("c","f","q","e","z ");
  3. array_multisort($a,$b);
  4. var_dump($a);
  5. var_dump($b);
  6. ?>
Copy the code

Run the results: array(5) { [0]=> int(0) [1]=> int(10) [2]=> int(50) [3]=> int(80) [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" }

Obviously z, which was originally the fifth element of array b, was ranked first.

In fact, to make it clear, array_multisort() first sorts the first array according to the size of the key values, and then adjusts the other arrays according to the adjustment strategy of the first array - the third element is placed first, and the Put the two elements in the second position...——In fact, the most basic embodiment of this multi-dimensional array sorting algorithm!

However, it should be noted that the number of elements in the two arrays must be the same, otherwise a warning message will appear: Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in ……

Okay, I hope everyone above can use it. Let’s talk about the main thing: array_multisort() sorts multi-dimensional arrays. This function will be very useful when doing projects in the future!

First, let’s take a look at the operation method of sorting each element [array] of a multi-dimensional array. It is very simple, but there are a few parameters that need to be explained. If you know something about SQL, you will probably understand it at a glance:

  1. //Let us construct a multi-dimensional array
  2. $a=array(100,2,4,7,7);
  3. $b=array('ab','ac' ,'ad','ag','ap');
  4. $ab = array($a,$b);
  5. //Start sorting
  6. array_multisort($ab[0],SORT_NUMERIC,SORT_DESC,$ab[1 ],SORT_STRING,SORT_ASC);
  7. print_r($ab);
  8. ?>
Copy the code

Explanation: First, we use SORT_NUMERIC to declare that $ab[0] is sorted by numeric type, and SORT_DESC The declaration order is reverse order (from large to small), and then we sort $ab[1] using the string type, and the order is ascending order (order) The final sorting result of array $ab is a combination of the two. First, sort in the reverse order of $ab[0]. If there are values ​​of the same size in $ab[0], they will be sorted in the order of $ab[1]. The output result is as follows: Array ( [0] => Array ( [0] => 100 [1] => 7 [2] => 7 [3] => 4 [4] => 2 ) [1] => Array ( [0] => ab [1] => ag [2] => ap [3] => ad [4] => ac ) ) Is it very similar to using order by in the database? In fact, it’s pretty much the same!

Now let’s look at a more practical example:

  1. $array[] = array("age"=>20,"name"=>"li");
  2. $array[] = array("age"=> ;21,"name"=>"ai");
  3. $array[] = array("age"=>20,"name"=>"ci");
  4. $array[] = array(" age"=>22,"name"=>"di");
  5. foreach ($array as $key=>$value){
  6. $age[$key] = $value['age'];
  7. $name[$key] = $value['name'];
  8. }
  9. array_multisort($age,SORT_NUMERIC,SORT_DESC,$name,SORT_STRING,SORT_ASC,$array);
  10. print_r($array);
  11. ?> ;
Copy code

The $array[] array in this example is constructed based on the records read from the database. We are now sorting them in order of age from largest to smallest. If the ages are the same, they are sorted in the order of names. This kind of sorting is what we will often use in the future. Because the sorting parameter required by array_multisort() must be a column, we use foreach to read out the age and name of this array. What happens next? Just like the example above, for sorting, you must have seen the last parameter $array. Yes, you need to declare which array to sort, because our first two parameters have nothing to do with the PHP array that needs to be sorted. , although in fact they are the data in $array - the columns we extracted from $array - sorting of course requires columns, and I have never seen row data used for sorting!

The output is as follows - just as we thought: Array ( [0] => Array ( [age] => 22 [name] => di ) [1] => Array ( [age] => 21 [name] => ai ) [2] => Array ( [age] => 20 [name] => ci ) [3] => Array ( [age] => 20 [name] => li ) )

You see, it’s actually very simple, it’s just that the few parameters that need to be capitalized are a bit annoying! Although it is a bit difficult to understand, it will be good if you understand it. It will be very useful in the future! appendix: Sort order flags: SORT_ASC – Sort in ascending order SORT_DESC – Sort in descending order

Sort type flag: SORT_REGULAR – compare items in the usual way SORT_NUMERIC – Compare items numerically SORT_STRING – Compare items by string

Two sorting flags of the same type cannot be specified after each array. The sort flags specified after each array are valid only for that array – before that the default values ​​SORT_ASC and SORT_REGULAR were used. Another point worth noting: this function changes the numeric index, leaving other indexes unchanged!

>>> For more information, please view the complete list of php array sorting methods



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template