我有一个像这样的数组
Array
(
[2013-03-12] => Array
(
[total_clicks] => 2266
[total_unique_clicks] => 177
)
[2013-03-19] => Array
(
[total_clicks] => 2647
[total_unique_clicks] => 241
)
[2013-03-20] => Array
(
[total_clicks] => 2656
[total_unique_clicks] => 245
)
)
现在我使用以下代码显示值
$current=strtotime($from);
$last=strtotime($to);
while($last >= $current){
$current_date=date("Y-m-d",$last);
echo $daily_click[$current_date]['total_unique_clicks'];
$last=strtotime("-1 day",$last);
}
这是显示值,但是如果日期没有值'2013-03-18',我需要显示索引为2013-03-12的数组的前一个值,值为177它实际上是一个运行总计,因此我需要在没有值的情况下显示其他日期。也就是说它应该显示之前的计数,直到它被更改为止。我怎么能这样做?
我需要这个日期的订单,因为我需要先显示最新日期并减少
答案 0 :(得分:1)
// get an array of the recorded dates in descending order
$descDateOrder = $daily_click;
ksort($descDateOrder);
$descDateOrder = array_keys(array_reverse($descDateOrder));
$current=strtotime($from);
$last=strtotime($to);
// initialize to the most recent date <= $last
$prev_key = 0;
for($i = 0; $i < count($descDateOrder); $i++) {
if (strtotime($descDateOrder[$i]) <= $last) {
$prev_key = $i;
break;
}
}
while($last >= $current) {
$current_date=date("Y-m-d",$last);
if ($descDateOrder[$prev_key] == $current_date) {
// use oldest date if we go past it (could break instead, to stop)
$prev_key = min(count($descDateOrder) - 1, $prev_key + 1);
} else {
$current_date = $descDateOrder[$prev_key];
}
echo $daily_click[$current_date]['total_unique_clicks'];
$last=strtotime("-1 day",$last);
}
答案 1 :(得分:0)
$current=strtotime($from);
$last=strtotime($to);
$array_key = array_keys($daily_click);
arsort($array_key);
while($last >= $current){
$current_date=date("Y-m-d",$last);
if($daily_click[$current_date]['total_unique_clicks'] != '')
{
echo $daily_click[$current_date]['total_unique_clicks'];
}
else
{
foreach( $array_key as $value)
{
if(strtotime($current_date) > strtotime($value))
{
$was_last_found = $value;
break;
}
}
echo $daily_click[$was_last_found]['total_unique_clicks'];
}
$last=strtotime("-1 day",$last);
}
答案 2 :(得分:0)
krsort($daily_clicks);
foreach($daily_clicks as $date => $val) {
if($date <= $to && $date >= $from)
echo $val['total_unique_clicks'];
}