Home > Article > Backend Development > PHP two-dimensional array sorting—default natural sorting
What is a two-dimensional array? A two-dimensional array is essentially an array with arrays as array elements, that is, "array of arrays", type specifier array name [constant expression] [constant expression]. A two-dimensional array is also called a matrix, and a matrix with the same number of rows and columns is called a variable square matrix. Symmetric matrix a[i][j] = a[j][i], diagonal matrix: There are zero elements outside the main diagonal of an n-order square matrix. PHP two-dimensional array sorting function, default natural sorting, that is, sort sorting. Here you can specify multiple methods to sort by a certain value in the two-dimensional array. See the program comments below for details.
/** * @function 二维数组自然排序 * @author www.phpernote.com * @param array $array 需要排序的数组(二维) * @param string key 需要根据哪个键排序 * @param string order 排序方式(SORT_ASC,SORT_DESC) * @param string type 排序方法(SORT_REGULAR,SORT_NUMERIC,SORT_STRING) * @return array sorted array. */ function phpSortArray($array,$key,$order="SORT_ASC",$type="SORT_REGULAR"){ if(!is_array($array)||empty($$array)){ return $array; } $ArgCount=func_num_args();//返回传递到目前定义函数的参数数目 for($I=1;$I<$ArgCount;$I++){ $Arg=func_get_arg($I); if(!eregi("SORT",$Arg)){ $KeyNameList[]=$Arg; $SortRule[]='$'.$Arg; }else{ $SortRule[]=$Arg; } } foreach($array AS $Key=>$Info){ foreach($KeyNameList as $KeyName){ ${$KeyName}[$Key]=$Info[$KeyName]; } } $EvalString='array_multisort('.join(",",$SortRule).',$array);'; eval($EvalString); return $array; }
The above is a tutorial on the default natural sorting of PHP two-dimensional array sorting. I hope it will be helpful to everyone.
Related recommendations:
PHP two-dimensional array assignment and traversal function implementation example
What is a PHP two-dimensional array? PHP II Detailed explanation of dimensional array examples
A PHP two-dimensional array sorting function sharing
The above is the detailed content of PHP two-dimensional array sorting—default natural sorting. For more information, please follow other related articles on the PHP Chinese website!