Home > Article > Backend Development > Introduction to PHP two-dimensional array sorting and default natural sorting methods_PHP tutorial
PHP two-dimensional array sorting function, the 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 two-dimensional array natural sorting
* @author www.phpernote.com
* @param array $array Array to be sorted (two-dimensional)
* @param string key The key to sort by
* @param string order sorting method (SORT_ASC, SORT_DESC)
* @param string type sorting method (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();//Returns the number of parameters passed to the currently defined function
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;
}
http://www.bkjia.com/PHPjc/326957.html