Sorting 2-D arrays or multidimensional arrays is a common problem, in PHP we have a special multidimensional array sorting function, the following briefly:
Array_multisort (array1,sorting order, sorting type,array2,array3. is a function that sorts multiple arrays or multidimensional arrays.
Array1 |
Necessary. A specified array of inputs. |
Sorting order |
Optional. Specify the order of arrangement. The possible values are SORT_ASC and Sort_desc. |
Sorting type |
Optional. Specify the sort type. The possible values are Sort_regular, Sort_numeric, and sort_string. |
Array2 |
Optional. A specified array of inputs. |
Array3 |
Optional. A specified array of inputs. |
The array in the argument is treated as a column of a table and sorted by row-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 compare to the same, they are sorted according to the size of the corresponding values in the next input array, and so on.
The first argument 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 arrangement order): SORT_ASC-Default, in ascending order. (A-Z) Sort_desc-in descending order. (Z-A)
You can then specify the sort type: sort_regular-Default. Arranges each item in a regular order. Sort_numeric-Arranges each item in numerical order. Sort_string-Alphabetical order of each item
[PHP] View Plain copy print? <?php function my_sort ($arrays, $sort _key, $sort _order= SORT_ASC, $sort _type=sort_numeric ) { if ( Is_array ($arrays)) { foreach ($arrays as $array) { if (Is_array ($array)) { $key _ arrays[] = $array [$sort _key]; }else{ return false; } } }else{ return false; } array_multisort ($key _arrays, $sort _ Order, $sort _type, $arrays); return $ arrays; } $person = array ( array (' id ' =>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 ' =>1), 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 ' => 1 ' name ' => ' FJ ' (length=2) ' weight ' => ' height ' => 180 1 =>Array(size=4) ' ID ' => 2 ' name ' => ' Tom ' (length=3) ' weight ' => ' height ' => 2 =>Array(size=4) ' ID ' => 3 ' name ' => ' Jerry ' (length=5) ' weight ' => ' height ' => 156 3 =>Array(size=4) ' ID ' => 4 ' name ' => ' Bill ' (length=4) ' weight ' => ' height ' => 190 4 =>Array(size=4) ' ID ' => 5 ' name ' => ' linken ' (length=6) ' weight ' => ' height ' => 5 =>Array(size=4) ' ID ' => 6 ' name ' => ' Madana ' (length=6) ' weight ' => ' height ' => 6 =>Array(size=4) ' ID ' => 7 ' name ' => ' Jordan ' (length=6) ' weight ' => ' height ' => 170
Array(size=7) 0 =>Array(size=4) ' ID ' => 4 ' name ' => ' Bill ' (length=4) ' weight ' => ' height ' => 190 1 =>Array(size=4) ' id ' => 1 ' name ' => ' FJ ' (length=2) ' weight ' => ' height ' => 180 2 =>Array(size=4) ' ID ' => 3 ' name ' => ' Jerry ' (length=5) ' weight ' => ' height ' => 156 3 =>Array(size=4) ' ID ' => 7 ' name ' => ' Jordan ' (length=6) ' weight ' => ' height ' => 170 4 =>Array(size=4) ' ID ' => 5 ' name ' => ' linken ' (length=6) ' weight ' => ' height ' => 5 =>Array(size=4) ' ID ' => 6 ' name ' => ' Madana ' (length=6) ' weight ' => ' height ' => 6 =>Array(size=4) ' ID ' => 2 ' name ' => ' Tom ' (length=3) ' weight ' => ' height ' => 150
Array(size=7) 0 =>Array(size=4) ' ID ' => 2 ' name ' => ' Tom ' (length=3) ' weight ' => ' height ' => 1 =>Array(size=4) ' ID ' => 7 ' name ' => ' Jordan ' (length=6) ' weight ' => ' height ' => 170 2 =>Array(size=4) ' ID ' => 5 ' name ' => ' linken ' (length=6) ' weight ' => ' height ' => 3 =>Array(size=4) ' ID ' => 6 ' name ' => ' Madana ' (length=6) ' weight ' => ' height ' => 4 =>Array(size=4) ' id ' => 1 ' name ' => ' FJ ' (length=2) ' weight ' => ' height ' => 180 5 =>Array(size=4) ' ID ' => 4 ' name ' => ' Bill ' (length=4) ' weight ' => ' height ' => 190 6 =>Array(size=4) ' ID ' => 3 ' name ' => ' Jerry ' (length=5) ' weight ' => ' height ' => 156
The point here is to save the key to a one-dimensional array, and then use the Array_multisort () function to sort the array by key, and, of course, you can simply not apply the Array_multisort () function in this sort of order, This can be achieved only through a foreach traversal, but now that the PHP developer has provided us with a better solution, we can dispense with unnecessary hassles.