Tips:点此可运行HTML源码

php 功能函数集

1.获取页面闭合带id标签数据

View Code
 1 <?php
 2     header("Content-type: text/html; charset=utf-8"); 
 3     /**
 4      * $tag_id  HTML tag_id like id="abc"
 5      * $url     web url
 6      * $tag     HTML tag
 7      * $data    HTML data if $url set to false
 8      * @example echo getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul');
 9      */
10     function getWebTag($tag_id,$url=false,$tag='div',$data=false){
11         if($url !== false){
12             $data = file_get_contents( $url );
13         }
14         $charset_pos = stripos($data,'charset');
15         if($charset_pos) {
16             if(stripos($data,'utf-8',$charset_pos)) {
17                 $data = iconv('utf-8','utf-8',$data);
18             }else if(stripos($data,'gb2312',$charset_pos)) {
19                 $data = iconv('gb2312','utf-8',$data);
20             }else if(stripos($data,'gbk',$charset_pos)) {
21                 $data = iconv('gbk','utf-8',$data);
22             }
23         }
24         
25         preg_match_all('/<'.$tag.'/i',$data,$pre_matches,PREG_OFFSET_CAPTURE);    //获取所有div前缀
26         preg_match_all('/<\/'.$tag.'/i',$data,$suf_matches,PREG_OFFSET_CAPTURE); //获取所有div后缀
27         $hit = strpos($data,$tag_id);
28         if($hit == -1) return false;    //未命中
29         $divs = array();    //合并所有div
30         foreach($pre_matches[0] as $index=>$pre_div){
31             $divs[(int)$pre_div[1]] = 'p';
32             $divs[(int)$suf_matches[0][$index][1]] = 's';    
33         }
34         
35         //对div进行排序
36         $sort = array_keys($divs);
37         asort($sort);
38         
39         $count = count($pre_matches[0]);
40         foreach($pre_matches[0] as $index=>$pre_div){
41             //<div $hit <div+1    时div被命中
42             if(($pre_matches[0][$index][1] < $hit) && ($hit < $pre_matches[0][$index+1][1])){
43                 $deeper = 0;
44                 //弹出被命中div前的div
45                 while(array_shift($sort) != $pre_matches[0][$index][1] && ($count--)) continue;
46                 //对剩余div进行匹配,若下一个为前缀,则向下一层,$deeper加1,
47                 //否则后退一层,$deeper减1,$deeper为0则命中匹配,计算div长度
48                 foreach($sort as $key){
49                     if($divs[$key] == 'p') $deeper++;
50                     else if($deeper == 0) {
51                         $length = $key-$pre_matches[0][$index][1];
52                         break;
53                     }else {
54                         $deeper--;
55                     }
56                 }
57                 $hitDivString = substr($data,$pre_matches[0][$index][1],$length).'</'.$tag.'>';
58                 break;
59             }
60         }
61         return $hitDivString;
62     }
63     
64     echo getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul');
65     echo getWebTag('id="homeBanners"','http://mail.163.com/html/mail_intro/');
66     echo getWebTag('id="performance"','http://mail.163.com/html/mail_intro/','section');
67 
68 //End_php

1.1 由(1)改进为获取页面任意标签,参考《颠覆想象的php解析获取跨域HTML标签

View Code
  1  <?php
  2     header("Content-type: text/html; charset=utf-8"); 
  3     /**
  4       * $tag_id  HTML tag_id like id="abc"
  5       * $url     web url
  6       * $tag     HTML tag
  7       * $data    HTML data if $url set to false
  8       * $first   Only get the first match
  9       * @example 
 10         var_dump(getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul'));
 11       */
 12     function getWebTag($tag_id,$url=false,$tag='div',$data=false,$first=false){
 13         //默认采用URL获取数据
 14         if($url !== false){
 15             $data = file_get_contents( $url );
 16         }
 17         //页面编码判定及转码
 18         $charset_pos = stripos($data,'charset');
 19         if($charset_pos) {
 20             if(stripos($data,'charset=utf-8',$charset_pos)) {
 21                 $data = iconv('utf-8','utf-8',$data);
 22             }else if(stripos($data,'charset=gb2312',$charset_pos)) {
 23                 $data = iconv('gb2312','utf-8',$data);
 24             }else if(stripos($data,'charset=gbk',$charset_pos)) {
 25                 $data = iconv('gbk','utf-8',$data);
 26             }
 27         }
 28 
 29         //匹配命中标签至数组$hits
 30         preg_match_all('/<'.$tag.'[^<]*?'.$tag_id.'/i',$data,$hits,PREG_OFFSET_CAPTURE);
 31         if(count($hits[0]) === 0) {                //未命中,直接返回
 32             return '没有匹配项!';
 33         }
 34 
 35         preg_match_all('/<'.$tag.'/i',$data,$pre_matches,PREG_OFFSET_CAPTURE);        //获取所有HTML标签前缀
 36         preg_match_all('/<\/'.$tag.'/i',$data,$suf_matches,PREG_OFFSET_CAPTURE);     //获取所有HTML标签后缀
 37 
 38         //判断是否<div></div>格式,是则添加结束标签,否则为false;  注:img、input等可能不是这种格式,此时$suf_matches[0]为空。
 39         if(!empty($suf_matches[0])) $endTag = '</'.$tag.'>';
 40         else $endTag = false;
 41         
 42         //合并所有HTML标签
 43         $htmltags = array();
 44         if($endTag !== false){
 45             foreach($pre_matches[0] as $index=>$pre_div){
 46                 $htmltags[(int)$pre_matches[0][$index][1]] = 'p';
 47                 $htmltags[(int)$suf_matches[0][$index][1]] = 's';
 48             }
 49         }else{
 50             foreach($pre_matches[0] as $index=>$pre_div){
 51                 //非<div></div>格式,获取前缀下标后的第一个>作为标签结束
 52                 $suf_matches[0][$index][1] = stripos($data,'>',$pre_matches[0][$index][1])+1;
 53                 
 54                 $htmltags[(int)$pre_matches[0][$index][1]] = 'p';
 55                 $htmltags[(int)$suf_matches[0][$index][1]] = 's';
 56             }
 57         }
 58         //对所有HTML标签按index进行排序
 59         $sort = array_keys($htmltags);
 60         asort($sort);
 61 
 62         //开始获取命中字符串
 63         $hitTagStrings = array();
 64         foreach($hits[0] as $hit){
 65             $hit = $hit[1];        //获取命中index
 66 
 67             $count = count($sort);    //循环控制,$count--避免无限循环
 68             foreach($pre_matches[0] as $index=>$pre_div){
 69                 //最后一个$pre_matches[0][$index+1]会造成数组出界,因此设置其index等于总长度
 70                 if(!isset($pre_matches[0][$index+1][1])) $pre_matches[0][$index+1][1] = strlen($data);
 71                 
 72                 //<div $hit <div+1    时div被命中
 73                 if(($pre_matches[0][$index][1] <= $hit) && ($hit < $pre_matches[0][$index+1][1])){
 74                     $deeper = 0;
 75                     //弹出被命中HTML标签前的所有HTML标签
 76                     while(array_shift($sort) != $pre_matches[0][$index][1] && ($count--)) continue;
 77                     //对剩余HTML标签进行匹配,若下一个为前缀(p),则向下一层,$deeper加1,
 78                     //否则后退一层,$deeper减1,$deeper为0则命中匹配结束标记,计算div长度
 79                     foreach($sort as $key){
 80                         if($htmltags[$key] == 'p') {    //进入子层
 81                             $deeper++;
 82                         }else if($deeper == 0) {        //碰到结束标记
 83                             $length = $key-$pre_matches[0][$index][1];        //长度等于结束标记index 减去 前缀index
 84                             break;
 85                         }else {                            //碰到子层结束标记
 86                             $deeper--;
 87                         }
 88                     }
 89                     $hitTagStrings[] = substr($data,$pre_matches[0][$index][1],$length).$endTag;
 90                     break;
 91                 }
 92             }
 93             //若只获取第一个匹配项,退出循环
 94             if($first && count($hitTagStrings) == 1) break;
 95         }
 96 
 97         return $hitTagStrings;
 98     }
 99     
100     //直接用例
101     var_dump(getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul'));
102 
103     /*        //注释这句即可显示
104     //ajax请求用例,必要参数:dataType:'json',type:'POST'
105     $tag_id     =     urldecode($_POST['tag_id']);
106     $url        =    urldecode($_POST['url']);
107     $tag        =    isset($_POST['tag'])? urldecode($_POST['tag']) : 'div';
108     $data        =    urldecode($_POST['data']);
109     $first        =    (urldecode($_POST['first']) == 'checked')? true : false;
110     foreach($_POST as $key => $value){
111         if($value == 'EmPtYValue') $$key = false;
112     }
113     echo json_encode(getWebTag($tag_id,$url,$tag,$data,$first));
114     //*/
115 
116 //End_php

 

2.虚拟POST数据至远程服务器并获取返回数据

View Code
 1 <?php
 2 header("Content-type: text/html; charset=utf-8"); 
 3 /**
 4  * $url  web url
 5  * $post POST data
 6  * @example 
 7     $data = array (
 8      'type' => 'text',
 9      'inputValue' => '哈哈'
10     );
11     $result = Post('http://tool.anzhuoxiazai.com:80//servlet/QRServlet', $data);
12     echo str_replace("src='","src='http://tool.anzhuoxiazai.com/",$result);
13  */
14 function Post($url, $post = null) {
15  $context = array();
16  if (is_array($post)) {
17      ksort($post);
18      $context['http'] = array (
19          'timeout'=>60,
20          'method' => 'POST',
21          'content' => http_build_query($post)
22      );
23  }
24  return file_get_contents($url, false, stream_context_create($context));
25 }
26 
27 $data = array (
28  'type' => 'text',
29  'inputValue' => '哈哈'
30 );
31 $result = Post('http://tool.anzhuoxiazai.com:80//servlet/QRServlet', $data);
32 echo str_replace("src='","src='http://tool.anzhuoxiazai.com/",$result);
33 //End_php

 

3.文件夹复制

View Code
 1 /** 
 2      * Copy file or folder from source to destination, it can do 
 3      * recursive copy as well and is very smart 
 4      * It recursively creates the dest file or directory path if there weren't exists 
 5      * Situtaions : 
 6      * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination 
 7      * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it 
 8      * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest      
 9      * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest 
10      * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name 
11      * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name 
12      * @todo 
13      *     - Should have rollback technique so it can undo the copy when it wasn't successful 
14      *  - Auto destination technique should be possible to turn off 
15      *  - Supporting callback function 
16      *  - May prevent some issues on shared enviroments : http://us3.php.net/umask 
17      * @param $source //file or folder 
18      * @param $dest ///file or folder 
19      * @param $options //folderPermission,filePermission 
20      * @return boolean 
21      */ 
22     function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755)) 
23     { 
24         $result=false; 
25         
26         if (is_file($source)) { 
27             if ($dest[strlen($dest)-1]=='/') { 
28                 if (!file_exists($dest)) { 
29                     cmfcDirectory::makeAll($dest,$options['folderPermission'],true); 
30                 } 
31                 $__dest=$dest."/".basename($source); 
32             } else { 
33                 $__dest=$dest; 
34             } 
35             $result=copy($source, $__dest); 
36             chmod($__dest,$options['filePermission']); 
37             
38         } elseif(is_dir($source)) { 
39             if ($dest[strlen($dest)-1]=='/') { 
40                 if ($source[strlen($source)-1]=='/') { 
41                     //Copy only contents 
42                 } else { 
43                     //Change parent itself and its contents 
44                     $dest=$dest.basename($source); 
45                     @mkdir($dest); 
46                     chmod($dest,$options['filePermission']); 
47                 } 
48             } else { 
49                 if ($source[strlen($source)-1]=='/') { 
50                     //Copy parent directory with new name and all its content 
51                     @mkdir($dest,$options['folderPermission']); 
52                     chmod($dest,$options['filePermission']); 
53                 } else { 
54                     //Copy parent directory with new name and all its content 
55                     @mkdir($dest,$options['folderPermission']); 
56                     chmod($dest,$options['filePermission']); 
57                 } 
58             } 
59 
60             $dirHandle=opendir($source); 
61             while($file=readdir($dirHandle)) 
62             { 
63                 if($file!="." && $file!="..") 
64                 { 
65                      if(!is_dir($source."/".$file)) { 
66                         $__dest=$dest."/".$file; 
67                     } else { 
68                         $__dest=$dest."/".$file; 
69                     } 
70                     //echo "$source/$file ||| $__dest<br />"; 
71                     $result=smartCopy($source."/".$file, $__dest, $options); 
72                 } 
73             } 
74             closedir($dirHandle); 
75             
76         } else { 
77             $result=false; 
78         } 
79         return $result; 
80     }

 

4.文件遍历(可匹配模式)

View Code
 1 /**
 2      * 遍历目录并获取所有目录即文件,以数组array('dirs'=>$dirs,'files'=>$files)方式返回。
 3      * @param $dir          //搜索目录
 4      * @param $pattern         // '*'搜索全部文件,可以智能匹配,如*.jpg 搜索jpg文件,*.{jpg,png}搜索jpg和png文件,区分大小写!!
 5      * @param $skip           //排除遍历文件,如"*.{jpg,png}"排除.jpg和.png类型文件
 6      * @param $subInclude   //默认遍历子目录,$subInclude设置为false则仅遍历当前目录
 7      * @param @flag            //glob函数的标记,有效标记如下:
 8             GLOB_MARK - 在每个返回的项目中加一个斜线 
 9             GLOB_NOSORT - 按照文件在目录中出现的原始顺序返回(不排序) 
10             GLOB_NOCHECK - 如果没有文件匹配则返回用于搜索的模式 
11             GLOB_NOESCAPE - 反斜线不转义元字符 
12             GLOB_BRACE - 扩充 {a,b,c} 来匹配 'a','b' 或 'c' 
13             GLOB_ONLYDIR - 仅返回与模式匹配的目录项 
14      */
15     function scandir_through($dir,$pattern='*',$skip=false,$subInclude=true,$flag=GLOB_BRACE){
16         $dirs = array();
17         $files = array();
18         //获取当前目录下所有文件及文件夹
19         $items  = glob($dir . '/*');
20 
21         //遍历所有项目,若设置$subInclude为true,则继续遍历子目录
22         for ($i = 0; $i < count($items); $i++) {
23             if ($subInclude && is_dir($items[$i])) {
24                 $dirs[] = iconv('gb2312','utf-8',$items[$i]);
25                 $add = glob($items[$i] . '/*');
26                 if($add === false) $add = array();
27                 $items = array_merge($items, $add);
28             }else {
29                 $slash = strrpos($items[$i],'/');
30                 $dir = substr($items[$i],0,$slash);
31                 //若当前文件匹配文件查找模式$pattern,则加入$files数组中
32                 if(in_array($items[$i],glob($dir.'/'.$pattern,$flag)) && (($skip===false) || !in_array($items[$i],glob($dir.'/'.$skip,$flag)))) {
33                     $items[$i] = iconv('gb2312','utf-8',$items[$i]);
34                     $file = substr($items[$i],$slash+1);
35                     $files[] = $items[$i];
36                 }
37             }
38         }
39         return array('dirs'=>$dirs,'files'=>$files);
40     }

 

5.zip压缩文件夹

View Code
 1 /**
 2  * 打包文件夹成zip文件函数
 3  * @param $path        //打包文件夹路径
 4  * @param $zip        //ZipArchive 对象
 5  */
 6 function addFileToZip($path, $zip) {
 7     $handler = opendir($path); //打开当前文件夹由$path指定。
 8     /*
 9     循环的读取文件夹下的所有文件和文件夹
10     其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename,
11     为了不陷于死循环,所以还要让$filename !== false。
12     一定要用!==,因为如果某个文件名如果叫'0',或者某些被系统认为是代表false,用!=就会停止循环
13     */
14     while (($filename = readdir($handler)) !== false) {
15         if ($filename != "." && $filename != "..") {//文件夹文件名字为'.'和‘..’,不要对他们进行操作
16             if (is_dir($path . "/" . $filename)) {// 如果读取的某个对象是文件夹,则递归
17                 addFileToZip($path . "/" . $filename, $zip);
18             } else { //将文件加入zip对象
19                 $zip->addFile($path . "/" . $filename);
20             }
21         }
22     }
23     @closedir($path);
24 }
25 //运用
26 $zip = new ZipArchive();
27 if ($zip->open('test.zip', ZipArchive::OVERWRITE) === TRUE) {
28     addFileToZip('./testdir', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
29     $zip->close(); //关闭处理的zip文件
30 }

 

 6.打印两个日期之间的日期

View Code
1 function prDates($start, $end) { 
2     //将ISO Date 转成 Timestamp
3     $dt_start = strtotime($start);
4     $dt_end   = strtotime($end);
5     do { 
6         //将 Timestamp 转成 ISO Date 输出
7         echo date('Y-m-d', $dt_start).PHP_EOL;
8     } while (($dt_start += 86400) <= $dt_end);    // 重复 Timestamp + 1 天(86400), 直至大于结束日期中止
9 }

 

 7.RC4加密算法

View Code
 1 /*
 2  * rc4加密算法,二次加密即可还原
 3  * $pwd 密钥
 4  * $data 要加密的数据
 5  */
 6 function rc4 ($pwd, $data)//$pwd密钥 $data需加密字符串
 7 {
 8     $key[] ="";
 9     $box[] ="";
10 
11     $pwd_length = strlen($pwd);
12     $data_length = strlen($data);
13 
14     for ($i = 0; $i < 256; $i++)
15     {
16         $key[$i] = ord($pwd[$i % $pwd_length]);
17         $box[$i] = $i;
18     }
19 
20     for ($j = $i = 0; $i < 256; $i++)
21     {
22         $j = ($j + $box[$i] + $key[$i]) % 256;
23         $tmp = $box[$i];
24         $box[$i] = $box[$j];
25         $box[$j] = $tmp;
26     }
27 
28     for ($a = $j = $i = 0; $i < $data_length; $i++)
29     {
30         $a = ($a + 1) % 256;
31         $j = ($j + $box[$a]) % 256;
32 
33         $tmp = $box[$a];
34         $box[$a] = $box[$j];
35         $box[$j] = $tmp;
36 
37         $k = $box[(($box[$a] + $box[$j]) % 256)];
38         $cipher .= chr(ord($data[$i]) ^ $k);
39     }
40     
41     return $cipher;
42 }

 

 8.utf8_substr UTF8编码字符串截取,解决中英文混杂截取

View Code
 1 /**
 2   * $sting                         输入字符串
 3   * $start                         截取起始位,默认为0
 4   * $length                        截取长度,中英文长度都为1,默认为'',跟输入负数一样截取至字符串结束。
 5   * [\x00-\x7F]                    匹配英文字符
 6   * [\xC0-\xFF][\x80-\xBF]+        匹配中文字符
 7   * 模式修正符s                     将换行当成普通字符,此时.*能匹配包括换行任意字符
 8   */
 9  function utf8_substr($string, $start=0, $length=''){
10      return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start.'}'
11      .'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$length.'}).*#s','$1',$string);
12  }

 

9.创建透明图片

$image = imagecreatetruecolor(300,300); 
$transparent = imagecolorallocatealpha($image, 255, 255, 255,127);    //创建透明背景色 
imagecolortransparent($img, $transparent);
imagefill($image, 0, 0, $transparent); //填充透明背景色
View Code

 

 10.图片灰白化

 1 <?php
 2 /**
 3  * 图片黑白处理
 4  * @params     $file          图片地址
 5  * @params    $output     直接输出
 6  * @params    $newFile    保存黑白图片
 7  * @usage
 8  1、imageGray('test.png');
 9  2、imageGray('test.png',false,'test_gray.png');
10  */
11 function imageGray($file,$output=true,$newFile=false){
12     if(preg_match('#\.(jpg|png|jpeg|gif)$#i',$file,$match)){
13         //不同类型图片读取函数
14         $create_function = 'imagecreatefrom'.$match[1];
15         //根据不同类型图片创建图片
16         $im = $create_function($file);
17         
18         //过滤成灰色图片
19         if($im) imagefilter($im, IMG_FILTER_GRAYSCALE);
20         
21         //不同类型图片写入函数
22         $image_function = 'image'.$match[1];
23         //输出到浏览器端
24         if ($output) {
25             header ("Content-type: image/".$match[1]);
26             $image_function($im);
27         }
28         //保存黑白照片
29         if($newFile) {
30             $file = $newFile;
31             $image_function($im,$file);
32         }
33         //删除图片资源
34         imagedestroy($im);
35     }
36     return $file;
37 }
38 ?> 
View Code

 

 11.域名授权认证

 1 function checkAuthenticated()
 2 {    
 3     //合法域名数组
 4     $domain_array = array(
 5         base64_encode(base64_encode('127.0.0.1')),
 6         base64_encode(base64_encode('localhost')),
 7         base64_encode(base64_encode('test.com')),
 8         base64_encode(base64_encode('*.test.com')),
 9     );
10     //对域名数组系列化并进行base64编码,为了安全,做了拼接处理,防止被直接解编码反系列化获取结果
11     $str = base64_encode(base64_encode(serialize($domain_array))."|".serialize($domain_array));
12     //解编码,分离拼接
13     $arr = explode("|",base64_decode($str));
14     //获取数据系列化部分,反系列化得到域名数组
15     $arr = unserialize($arr[1]);
16     //遍历域名数组,解编码获取域名
17     foreach($arr as $k=>$v)
18     {
19         $arr[$k] = base64_decode(base64_decode($v));
20     }
21     //获取当前URL的host地址
22     $host = $_SERVER['HTTP_HOST'];
23     //host:端口 分离
24     $host = explode(":",$host);
25     //去除端口部分
26     $host = $host[0];
27     //认证通过标志,默认未通过认证
28     $passed = false;
29     //遍历域名数组
30     foreach($arr as $k=>$v)
31     {
32         //带通配子域名情况,使用preg_match进行匹配检测
33         if(substr($v,0,2)=='*.')
34         {
35             $preg_str = substr($v,2);    //去掉通配部分
36             if(preg_match("/".$preg_str."$/",$host)>0)    //preg_match('/test.com$/','www.test.com') ,$preg_str中的.可匹配任意字符,包括.
37             {
38                 $passed = true;        //匹配通过,退出循环
39                 break;
40             }
41         }
42     }
43     if(!$passed)        //标志为假,因为foreach中为判断非通配域名(test.com),因此还需要进行一次in_array判断
44     {
45         if(!in_array($host,$arr))
46         {
47             return false;
48         }
49     }
50     return true;
51 }
View Code

 

12.获取文件权限值

1 function getChmod($filepath){
2     return substr(base_convert(@fileperms($filepath),10,8),-4);
3 }

 

13.数组不定长多键值排序

 1 <?php
 2 /**
 3  * PHP数组不定长多键值排序
 4  *
 5  * @param array $list     数据源
 6  * @param array $rules    排序规则 ['key1'=>'asc', 'key2' => 'desc', ...]
 7  * @return array
 8  */
 9 function smartMultiSort($list, $rules) {
10     $multisortParams = [];
11     foreach($rules as $key => $sort) {
12         $multisortParams[$key] = [];
13         $multisortParams[$key . $sort] = constant(strtoupper("sort_{$sort}"));
14     }
15     foreach($list as $item) {
16         foreach($rules as $key => $sort) {
17             $multisortParams[$key][] = $item[$key];
18         }
19     }
20     $multisortParams[] = &$list;
21 
22     call_user_func_array('array_multisort', $multisortParams);
23     return array_pop($multisortParams);
24 }
25 
26 //示例
27 $list = array ();
28 $list [] = array (
29         'id' => 1,
30         'name' => '学生1',
31         'school' => '学校1',
32         'class' => '班级1'
33 );
34 $list [] = array (
35         'id' => 4,
36         'name' => '学生4',
37         'school' => '学校2',
38         'class' => '班级2'
39 );
40 $list [] = array (
41         'id' => 3,
42         'name' => '学生3',
43         'school' => '学校2',
44         'class' => '班级1'
45 );
46 $list [] = array (
47         'id' => 2,
48         'name' => '学生2',
49         'school' => '学校1',
50         'class' => '班级2'
51 );
52 $list [] = array (
53         'id' => 5,
54         'name' => '学生5',
55         'school' => '学校2',
56         'class' => '班级3'
57 );
58 print_r(smartMultiSort($list, ['school' => 'desc','id' => 'asc']));
View Code

 

posted @ 2012-09-05 12:05 Zjmainstay 阅读(...) 评论(...) 编辑 收藏
实现请参考《为博客园添加标签云动画