Home >Backend Development >PHP Tutorial >Introduction to PHP two-dimensional array sorting and default natural sorting methods
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; }
For more introduction to PHP two-dimensional array sorting and default natural sorting methods, please pay attention to the PHP Chinese website!