This article mainly introduces PHP implementation of a two-dimensional array by a column to sort the method, combined with an example of PHP two-dimensional array sequencing techniques, involving the use of Array_multisort functions, the need for friends can refer to the next
Specific as follows:
/** a two-dimensional array sorted by a column * Array_multisort ($arr 1, $arr 2) * The manual example is as follows **/$data [] = Array (' volume ' = ' + ', ' edition ' = 2); $data [] = AR Ray (' volume ' = +, ' edition ' + 1); $data [] = Array (' volume ' = = ' Edition ' = 6); $data [] = Array (' volume ' = > 98, ' Edition ' + 2); $data [] = Array (' volume ' = +, ' Edition ' = + 6); $data [] = Array (' volume ' = ' + ', ' edit Ion ' = + 7);//Gets the list of columns foreach ($data as $key + $row) {$volume [$key] = $row [' volume ']; $edition [$key] = $row [' edition '];} Sort the data in descending order of volume, in ascending order according to edition//$data as the last parameter, sorted with Universal key Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $dat a);/*************************************//* from the above example, it is not difficult to see that the Array_multisort () function * is to sort the columns of each two-dimensional array as a parameter * to achieve the ordering of 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 (The Sigcol_arrsort ($data, ' edition ', Sort_desc));/* The principle is: The new array original two-dimensional array 2 array (' volume ' = +, ' edition ' = 2); 1 Array (' volume ' = +, ' Edition ' = + 1); 6 Array (' Volume ' = ' Edition ' = 6); 2 Array (' volume ' = = 98, ' edition ' = 2); 6 Array (' volume ' = +, ' Edition ' = + 6); 7 Array (' volume ' = +, ' Edition ' = + 7); Each row of the two arrays is locked together, and once the new array is in the order of change, then the two-dimensional array of each row is followed by the transformation order, that is, the new array is the reference to order B in the query clause y*/
Operation Result:
Array ( [0] = = Array ( [Volume] = [ Edition] = 7 ) [1] = = Array ( [ Volume] = [Edition] + 6 ) [2] = = Array ( [Volume] = [ Edition] = 6< c14/>) [3] = = Array ( [Volume] = [ Edition] = 2 ) [4] = = Array ( [Volume] = 98 [Edition] + 2 ) [5] = = Array ([volume] = [ Edition] = 1 ))
The above is the whole content of this article, I hope that everyone's study has helped.