This article mainly introduces PHP's method of sorting two-dimensional arrays by a column, and analyzes the php's two-dimensional array sorting technique in the form of examples, involving the use of the array_multisort function, for more information about how to sort two-dimensional arrays by column in PHP, see the following example. We will share this with you for your reference. The details are as follows:
/** Sort two-dimensional arrays by column * array_multisort ($ arr1, $ arr2) * example of the manual **/$ data [] = array ('Volume '=> 67, 'version' => 2); $ data [] = array ('Volume '=> 86, 'version' => 1 ); $ data [] = array ('Volume '=> 85, 'version' => 6); $ data [] = array ('Volume' => 98, 'version' => 2); $ data [] = array ('Volume '=> 86, 'version' => 6 ); $ data [] = array ('Volume '=> 67, 'version' => 7); // get the column list foreach ($ data as $ key => $ row) {$ volume [$ key] = $ row ['Volume ']; $ edition [$ key] = $ row ['version'];} // sort data by volume in descending order and edition in ascending order // use $ data as the last parameter and sort array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data ); /*************************************// * as shown in the preceding example, array_multisort () the function * sorts every two-dimensional array column as a parameter * to sort the entire array *//**************** * *******************/function sigcol_arrsort ($ data, $ col, $ type = SORT_DESC) {if (is_array ($ data) {$ I = 0; foreach ($ data as $ k => $ v) {if (key_exists ($ col, $ v) {$ arr [$ I] = $ v [$ col]; $ I ++ ;}else {continue ;}}} else {return false;} array_multisort ($ arr, $ type, $ data); return $ data;} print_r (sigcol_arrsort ($ data, 'version', SORT_DESC )); /* to put it bluntly, the new array's original two-dimensional array 2 array ('Volume '=> 67, 'version' => 2); 1 array ('Volume' => 86, 'version' => 1); 6 array ('Volume '=> 85, 'version' => 6); 2 array ('Volume' => 98, 'version' => 2); 6 array ('Volume '=> 86, 'version' => 6); 7 array ('Volume' => 67, 'version' => 7); each row of the two arrays is locked together. Once the order of the new array changes, the two-dimensional arrays of each row are also changed, that is, the new array is equivalent to the order */
Running result:
Array( [0] => Array ( [volume] => 67 [edition] => 7 ) [1] => Array ( [volume] => 85 [edition] => 6 ) [2] => Array ( [volume] => 86 [edition] => 6 ) [3] => Array ( [volume] => 67 [edition] => 2 ) [4] => Array ( [volume] => 98 [edition] => 2 ) [5] => Array ( [volume] => 86 [edition] => 1 ))
I hope this article will help you with PHP programming.
For more articles about how to sort two-dimensional arrays by column in PHP, refer to PHP Chinese website!