We know that in the PHP array is divided into multi-dimensional arrays and one-dimensional arrays, we will explain the PHP multidimensional array and one-dimensional array sorting principle and implementation method.
One-dimensional arrays
The first group: sort and Rsort, sorted by the order of the PHP array key values ASC and reverse desc, while destroying the index relationship of the original array-in fact, after dropping the index, re-establishes the 0-based numeric index. Take a look at the routines:
The code is as follows |
Copy Code |
$a = Array ("a" =>1,2); Sort ($a); Var_dump ($a);
Rsort ($a); Var_dump ($a); ?> Take a look at the first output, the first output: Array (2) { [0]=> Int (1) [1]=> Int (2) } A second output: Array (2) { [0]=> Int (5) [1]=> Int (4) }
|
Where did you find the index a we originally defined? Where did it go? To be sure, they have been ruthlessly deleted, if you do not care about the original index relationship, you can use them!
The second set of functions: Asort and Arsort, the two functions are a bit more powerful, as long as they can preserve the original index of the array, the above example of the sort and rsort with the two functions, respectively, to see the results of the operation:
The code is as follows |
Copy Code |
Array (2) { ["A"]=> Int (1) [0]=> Int (2) } Array (2) { [0]=> Int (2) ["A"]=> Int (1) }
|
This one can understand, needless to say!
The third set of PHP array sorting functions: Krsort and Ksort These two are different from the above two groups, these two functions are the key name of the order, we can replace the above example of the function of these two, to see the specific operation results, here also do not say, otherwise this article is too long to write, Afraid that some brothers have no patience to see the focus of this article, although the focus is below!
Using a custom function to sort the PHP array, there are three functions:
Uasort uses a custom function to sort the key values of the PHP array and preserves the original index relationship.
Uksort uses a custom function to sort the key names of the PHP array and preserves the original index relationship.
Usort sorts the key values of the PHP array by using a custom function, and deletes the original index relationship, creating a new index from scratch.
This place certainly needs an example:
The code is as follows |
Copy Code |
Output Result: Array (4) { [0]=> Int (1) [3]=> Int (5) [1]=> Int (4) [2]=> Int (3) }
|
,
Ordering of multidimensional arrays
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:
The code is as follows |
Copy Code |
$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:
The code is as follows |
Copy Code |
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:
|
copy code |
$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:
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, 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!
Appendix:
Sort order Flags:
sort_asc– Sort by ascending order
sort_desc– Sorted in descending order
Sort Type flag:
sort_regular– Compare projects by usual method
sort_numeric– the project by numerical comparison
sort_string– to compare items by string
http://www.bkjia.com/PHPjc/633095.html www.bkjia.com true http://www.bkjia.com/PHPjc/633095.html techarticle we know that in the PHP array is divided into multi-dimensional arrays and one-dimensional arrays, we will explain the PHP multidimensional array and one-dimensional array sorting principle and implementation method. One-dimensional array first set ...