For example:
1 //The original array is like this, and you want to be able to sort by run_date ascending or descending in the second dimension:2 $arr=Array(30=>Array(4' Run_date ' = ' 2017-11-21 ',5' Count ' = ' 5 '6),71=>Array(8' Run_date ' = ' 2017-11-20 ',9' Count ' = ' 10 'Ten), One2=>Array( A' Run_date ' = ' 2017-11-22 ', -' Count ' = ' 10 ' - ) the ); - //want to get: - $arr=Array( -0=>Array( +' Run_date ' = ' 2017-11-20 ', -' Count ' = ' 5 ' +), A1=>Array( at' Run_date ' = ' 2017-11-21 ', -' Count ' = ' 10 ' -), -2=>Array( -' Run_date ' = ' 2017-11-22 ', -' Count ' = ' 10 ' in ) -);
Of course, it can be solved with loop traversal, but still feel a little bit inefficient, here is another solution, is to use two PHP provided functions:
array_column , array_multisort
or the above array:
//First Use Array_column multidimensional array to be taken out by vertical (column)$date= Array_column ($arr, ' Run_date ');//results obtained above: Array (0=> ' 2017-11-21 ',1=> ' 2017-11-20 ',3=> ' 2017-11-22 ')//Array_multisort combined Array_ Column results are sorted by $arrArray_multisort($date, SORT_ASC,$arr);//The final result:$arr=Array( 0=>Array( ' Run_date ' = ' 2017-11-20 ', ' count ' = ' 5 ' ), 1=>Array( ' Run_date ' = ' 2017-11-21 ', ' count ' = ' 10 ' ), 2=>Array( ' Run_date ' = ' 2017-11-22 ', ' count ' = ' 10 ' ) );
Report:
1,array_column reference my previous blog http://www.cnblogs.com/Steven-shi/p/5897745.html
2. Thearray_multisort array_multisort () function sorts multiple arrays or multidimensional arrays and can be used to sort multiple PHP two-dimensional arrays at once, or to sort multidimensional arrays based on a dimension or multidimensional
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-Arranges each item alphabetically.
Array_multisort (array1,sorting order,sorting type,array2,array3 ...)
Array1: Required. 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.
Note: The string key name is retained, but the number key is re-indexed, starting at 0 and incrementing by 1.
Note: You can set the sort order and sort type after each array. If not set, the default value is used for each array parameter.
The PHP two-dimensional array is sorted by the first array, and the subsequent order is adjusted. Arrays like this:
Array Array (1,3,2), ' data ' = =array(' A ', ' C ', ' B ')
As long as the ID of the multidimensional sort, you can. But most of the time, the array we construct is this:
Array Array Array Array (' id ' =>2, ' data ' = ' B '));
The elements of the PHP two-dimensional array are sorted by row and need to be sorted by one of the columns. PHP does not seem to provide a function like a matrix transpose, so it is not possible to use Array_multisort directly for multidimensional sorting. But you just need to extract the sorted column first, and pass it to Array_multisort as the first parameter.
PHP sorts a two-bit array by the value of an element in the second dimension