dpf5207 2013-07-25 05:10
浏览 40
已采纳

将关联数组与日期排序为日期顺序

So I have an associative array that's 1 level deep (excerpt as below) but there are a lot of entries

[0] => Array
            (
                [Electronic] => 1
                [Scope] => Intruder Alarm Systems                                                                              
                [Issued Date] => 2013-07-23 01:03:41
                [Customer Name] => qqqq
                [Certificate Number] => 1291087
            )

        [1] => Array
            (
                [Electronic] => 1
                [Scope] => CCTV Systems                                                                                        
                [Issued Date] => 2013-07-23 01:02:01
                [Customer Name] => qqqqq
                [Certificate Number] => 1291085
            )

        [2] => Array
            (
                [Electronic] => 1
                [Scope] => CCTV Systems                                                                                        
                [Issued Date] => 2013-07-17 07:15:06
                [Customer Name] => Accent Foundation Ltd
                [Certificate Number] => 1290822
            )

I need a way of looping through this array and re-ordering it so the newest is first and so on. Basically as if you had selected it from a DB and used "ORDER BY "Issued Date" DESC"

I can't really think of a way of doing this.

  • 写回答

2条回答 默认 最新

  • dppxp79175 2013-07-25 05:16
    关注

    You can use usort with your own custom function to sort.

    usort($array, function ($a, $b) {
         $atime = strtotime($a['Issue Date']);
         $btime = strtotime($b['Issue Date']);
         return $atime - $btime;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部