php二维数组排序与默认自然排序的方法介绍_php实例

WBOY
Release: 2016-06-07 17:24:42
Original
829 people have browsed it

php二维数组排序函数,默认自然排序,即sort排序。这里可以指定按二维数组中的某个值进行多种方法排序,具体看下面的程序注释。

复制代码 代码如下:

/** 

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

         $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; 

 }


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template