For a 2-dimensional array or multi-dimensional array sorting is a common problem, in PHP we have a special multi-dimensional array sorting function, the following simple introduction:
Array_multisort (array1,sorting order, sorting type,array2,array3. ) is a function that sorts multiple arrays or multidimensional arrays.
| Array1 |
Necessary. Specifies the input array. |
| Sorting order |
Optional. Specify the order of arrangement. The possible values are SORT_ASC and Sort_desc. |
| Sorting type |
Optional. Specifies the sort type. The possible values are Sort_regular, Sort_numeric, and sort_string. |
| Array2 |
Optional. Specifies the input array. |
| Array3 |
Optional. Specifies the input array. |
The array in the parameter is treated as a column of a table and sorted by rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. If the rows (values) in the array are the same, they are sorted according to the size of the corresponding values in the next input array, and so on.
The first parameter is an array, and each subsequent argument may be an array, or it may be one of the following sort order flags (the sort flags are used to change the default order):
SORT_ASC-By default, in ascending order. (A-Z)
Sort_desc-sorted in descending order. (Z-A)
You can then specify the sort type:
Sort_regular-Default. Arranges each item in a regular order.
Sort_numeric-Sorts each item in numerical order.
Sort_string-Alphabetical order of each item
1, ' name ' = ' fj ', ' weight ' =>100, ' height ' =>180), array (' ID ' =>2, ' name ' = ' tom ', ' Weight ' =>53, ' Height ' =>150 ', array (' ID ' =>3, ' name ' = ' Jerry ', ' Weight ' =>120, ' height ' =>156), array (' ID ' =>4, ' Name ' = ' Bill ', ' Weight ' =>110, ' height ' =>190), array (' ID ' =>5, ' name ' = ' linken ', ' weight ' =>80, ' Height ' =>200 ', array (' ID ' =>6, ' name ' = ' madana ', ' weight ' =>95, ' height ' =>110), array (' ID ' =>7, ' Name ' = ' Jordan ', ' weight ' =>70, ' height ' =>170); Var_dump ($person); $person = My_sort ($person, ' name ', Sort_ asc,sort_string); Var_dump ($person); $person = My_sort ($person, ' weight '); Var_dump ($person);? >
The results are as follows:
Array(size=7) 0 =Array(size=4) ' id ' = = int 1 ' name ' = = String ' FJ ' (length=2) ' weight ' + int ' height ' = = int 1 =Array(size=4) ' id ' = = int 2 ' name ' = = String ' Tom ' (length=3) ' weight ' = ' = Int ' height ' = = int 2 =Array(size=4) ' id ' = = int 3 ' name ' = + string ' Jerry ' (length=5) ' weight ' = ' = Int ' height ' = ' = int 156 3 =& GtArray(size=4) ' id ' = = int 4 ' name ' = + string ' Bill ' (length=4) ' weight ' = ' = Int ' height ' = = int 4 =&G TArray(size=4) ' ID ' + int 5 ' name ' = = String ' linken ' (length=6) ' weight ' + int ' height ' = = int 200 5 =& GtArray(size=4) ' ID ' + int 6 ' name ' = ' + string ' Madana ' (length=6) ' weight ' = ' = Int ' height ' = + int 110 6 =& GtArray(size=4) ' id ' = = int 7 ' name ' = + string ' jordan ' (length=6) ' weight ' = = Int ' height ' + int 170
Array(size=7) 0 =Array(size=4) ' id ' = = int 4 ' name ' = + string ' Bill ' (length=4) ' weight ' = ' = Int ' height ' = = int 1 =&G TArray(size=4) ' id ' = = int 1 ' name ' = = String ' FJ ' (length=2) ' weight ' + int ' height ' = = int 2 =Array(size=4) ' id ' = = int 3 ' name ' = + string ' Jerry ' (length=5) ' weight ' = ' = Int ' height ' = ' = int 156 3 =& GtArray(size=4) ' id ' = = int 7 ' name ' = + string ' jordan ' (length=6) ' weight ' = ' = Int ' height ' = = int 170 4 =& GtArray(size=4) ' ID ' + int 5 ' name ' = = String ' linken ' (length=6) ' weight ' + int ' height ' = = int 200 5 =& GtArray(size=4) ' ID ' + int 6 ' name ' = ' + string ' Madana ' (length=6) ' weight ' = ' = Int ' height ' = + int 110 6 =& GtArray(size=4) ' id ' = = int 2 ' name ' = + string ' Tom ' (length=3) ' weight ' = ' = Int ' height ' + int 150
Array(size=7) 0 =Array(size=4) ' id ' = = int 2 ' name ' = = String ' Tom ' (length=3) ' weight ' = ' = Int ' height ' = = int 1 =Array(size=4) ' id ' = = int 7 ' name ' = + string ' jordan ' (length=6) ' weight ' = ' = Int ' height ' = = int 170 2 =& GtArray(size=4) ' ID ' + int 5 ' name ' = = String ' linken ' (length=6) ' weight ' + int ' height ' = = int 200 3 =& GtArray(size=4) ' ID ' + int 6 ' name ' = ' + string ' Madana ' (length=6) ' weight ' = ' = Int ' height ' = + int 110 4 =& GtArray(size=4) ' id ' = = int 1 ' name ' = = String ' FJ ' (length=2) ' weight ' + int ' height ' = = Int 5 =Array(size=4) ' id ' = = int 4 ' name ' = + string ' Bill ' (length=4) ' weight ' = ' = Int ' height ' = = int 6 =&G TArray(size=4) ' id ' = = int 3 ' name ' = + string ' Jerry ' (length=5) ' weight ' = = int + ' height ' = = int 156
The point here is that the key to be sorted is stored in a one-dimensional array, then you can use the Array_multisort () function to sort the array by key, of course, the sort you can not apply array_multisort () This function, This effect can be achieved only with a foreach traversal, but since PHP developers have provided us with a better approach, we can eliminate unnecessary hassles.