php 按日期查询 补全日期,用php补全数组中的日期

终点应放在原始数组中,不然算法不可能自作主张的添加数据

$a = array(

array('period' => 201303, 'out_count' => 383),

array('period' => 201304, 'out_count' => 43),

array('period' => 201306, 'out_count' => 2859),

array('period' => 201307, 'out_count' => 251),

array('period' => 201309, 'out_count' => 807),

);

$a[] = array('period' => 201312, 'out_count' => 0); //添加终点

$last = '';

foreach($a as $r) {

while($last && $last 

$res[] = array('period' => $last, 'out_count' => 0);

$last = date('Ym', $last = strtotime("+1 month {$last}01"));

}

$res[] = $r;

$last = date('Ym', strtotime("+1 month {$r['period']}01"));

}

print_r($res);

Array

(

[0] => Array

(

[period] => 201303

[out_count] => 383

)

[1] => Array

(

[period] => 201304

[out_count] => 43

)

[2] => Array

(

[period] => 201305

[out_count] => 0

)

[3] => Array

(

[period] => 201306

[out_count] => 2859

)

[4] => Array

(

[period] => 201307

[out_count] => 251

)

[5] => Array

(

[period] => 201308

[out_count] => 0

)

[6] => Array

(

[period] => 201309

[out_count] => 807

)

[7] => Array

(

[period] => 201310

[out_count] => 0

)

[8] => Array

(

[period] => 201311

[out_count] => 0

)

[9] => Array

(

[period] => 201312

[out_count] => 0

)

)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值