php-以日期为键对数组进行排序 [英] php - Sort array with date as key

查看:88
本文介绍了php-以日期为键对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其中的键是这种格式的日期.

Hi I have an array with keys as date in this format.

$arr = array(

    "20110805" => "2",
    "20100703" => "5",
    "20110413" => "3",
    "20100805" => "4",
    "20100728" => "6",
    "20090416" => "7",
    "20080424" => "8",
    "20110819" => "1",  
);

如何按键对数组进行排序.谢谢.

how can I sort this array by key. Thank you.

推荐答案

使用这种格式的日期,按字母顺序进行比较就可以了.使用PHP函数 ksort .

With the dates in that format, an alphabetical comparison will work just fine. Use the PHP function ksort.

ksort($arr);

这篇关于php-以日期为键对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆