php - 以日期为键排序数组

时间:2021-08-22 21:33:29

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.

我怎么能按键排序这个数组。谢谢。

2 个解决方案

#1


20  

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

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

ksort($arr);

#2


4  

Just this single line of code:

只需这一行代码:

ksort($arr);

#1


20  

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

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

ksort($arr);

#2


4  

Just this single line of code:

只需这一行代码:

ksort($arr);