Home >Backend Development >PHP Tutorial >Introduction to PHP two-dimensional array sorting and default natural sorting methods_PHP tutorial

Introduction to PHP two-dimensional array sorting and default natural sorting methods_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:11:09864browse

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.

Copy code The code is as follows:

/**

* @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

truehttp: //www.bkjia.com/PHPjc/326957.htmlTechArticlephp 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. Copy...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn