我基本上有这个代码来确定数组中相同日期的数量:
function get_archives_html ($blog) {
//tag array to store arrays with same tag
$count_tags = array();
//store output
$output = '';
foreach($blog as $id) {
//abbreviate date name
$originalDate = $id["date"]["year"].'-'.$id["date"]["month"].'-'.$id["date"]["day"];
$abbrevDate = date("F, Y", strtotime($originalDate));
if(!isset($count_tags[$abbrevDate])) {
$count_tags[$abbrevDate] = 0;
}
++$count_tags[$abbrevDate];
}
// Sort your tags from hi to low
//arsort($count_tags);
var_dump($count_tags);
foreach($count_tags as $month=>$id) {
$output.= '<p><a href="#">'. $month.($id > 1 ? ' ('.$id .')' : '').'</a></p>';
}
return $output;
}
输出如下:
$arr = array(
"November, 2016" => "2",
"October, 2016" => "5",
"October, 2017" => "3",
"September, 2017" => "6"
);
现在,我使用键在html上显示它。问题是它没有按日期排列,而是按字母排列。
所以,我的问题是,如何按键和按日期对此数组进行排序。示例将是2016年10月,2016年11月,2017年9月,2014年10月谢谢
答案 0 :(得分:1)
<?php
$arr = array(
"November, 2016" => "2",
"October, 2016" => "5",
"October, 2017" => "3",
"September, 2017" => "6"
);
//Sort by ascending date in the key
uksort($arr,function($a,$b){
return strtotime(strtr($a,',',' '))<=>strtotime(strtr($b,',',' '));
});
//result
$expected = array (
'October, 2016' => "5",
'November, 2016' => "2",
'September, 2017' => "6",
'October, 2017' => "3",
);
var_dump($arr === $expected); //bool(true)