Home > Article > Backend Development > Learn to sort multidimensional arrays in php
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:
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:
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 |