zoukankan      html  css  js  c++  java
  • php多维数组排序

    问题:

    有这样一个数组$arr:

    [1]=> array(3) { [0]=> string(89) "........./r1822113797800000_AIK1_20140626_190411.mp3" [1]=> string(4) "AIK1" [2]=> string(16) "18221*********" }
    [2]=> array(3) { [0]=> string(89) "........./r1822113797800000_AIK1_20140626_190422.mp3" [1]=> string(4) "AIK1" [2]=> string(16) "1822*********" }  
    ......

    ......

    要求按照时间20140626_190411来排序

    这个用到了php的多维数组排序array_mutisort()函数,用法如下:

    foreach($arr as $key=>$row)
                            {
                                preg_match("/[0-9]{4}_[0-9]{6}/",$row[0],$match);//使用正则表达从"........./r1822113797800000_AIK1_20140626_190422.mp3"中匹配出时间20140626_190422
                                $date_for_sort[$key] = $match[0];//将时间作为排序的关键字
                            }
    array_multisort($date_for_sort,SORT_ASC,$arr);//使用$date_for_sort作为关键字对数组$arr进行升序排序

    这个函数相当的好用呀~

     数组内容为中文的话,需要编码转换为GBK或者GB2312形式,它们本身就使用拼音排序的,用到了iconv()函数:

    foreach($arrbelong2 as $key=>$row)
                            {                            
                                $belong2_for_sort[$key] = iconv('UTF-8', 'GB2312',$row['fullname']);
                            }
     array_multisort($belong2_for_sort,SORT_ASC,$arrbelong2);
    -------------------------------- |原来看似困难的事情是那么的简单ᶘ ᵒᴥᵒᶅ| --------------------------------
  • 相关阅读:
    Docker基础 ubuntu安装docker
    layui.laytpl 模板引擎用法
    golang 中 strings 包的 Replace 用法介绍笔记
    golang之结构体
    Mysql复习秘籍
    NFS 共享存储
    Rsyncd 同步服务
    企业服务器架构
    基础面试题二
    基础面试题一
  • 原文地址:https://www.cnblogs.com/Sophiawow/p/3833480.html
Copyright © 2011-2022 走看看