用php补全数组中的日期 我的想法:首先Count(array())的个数,如果小于12,则从第一个字符串开始右截取其2位(比如201301的01),看其是否==01,如果不是,则自动添加从201301到该字符串,如果是,则同时右截取下一个字符串,并判断是否为上一字符串 1,如果是,则自动继续后面的,不是,则补齐中间空缺的字符串。于此类推。 中间可能需要用到字符串和数字转换的函数。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 补齐之后每个out_count都为0 了? 那你直接构建一个不就行了?$m=range(201301,201312);foreach($m as $v){ $arr[]=array('period' =>$v,'out_count' =>0);}print_r($arr); $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),);$last = '';foreach($a as $r) { while($last && $last < $r['period']) { $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] => 0 ) [4] => Array ( [period] => 201307 [out_count] => 251 ) [5] => Array ( [period] => 201308 [out_count] => 0 ) [6] => Array ( [period] => 201309 [out_count] => 807 )) 终点应放在原始数组中,不然算法不可能自作主张的添加数据$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 < $r['period']) { $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 )) Quote: 引用 10 楼 xuzuning 的回复:终点应放在原始数组中,不然算法不可能自作主张的添加数据[code=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 < $r['period']) { $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"));}好的 谢谢老大 如果1~12月都补的话···能实现么 array_unshift($a,array('period' => 201301, 'out_count' => 0)); //这样就添加了起点 好的 谢谢各位··换了个思路 我把时间改成在sql语句中实现了 按每个月group by date wordpress中如何让每个页面单独于首页显示出来啊? red5 声音小了录不上去 php 网站打开为何为代码 一个PHP登陆注入小示例 PHP实现计划任务控制 smarty section怎么结束循环 GuaGua小型论坛系统2.2.0正式版_免费开源_下载 大家推荐一款和php结合的比较好的js框架吧?在线等待,急用,谢谢了 phpCAS 单点登录 nginx有没像php_value一样的功能啊? 朋友想搞个门户网站,有什么开源的cms推荐 求解救
$m=range(201301,201312);
foreach($m as $v){
$arr[]=array('period' =>$v,'out_count' =>0);
}
print_r($arr);
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),
);$last = '';
foreach($a as $r) {
while($last && $last < $r['period']) {
$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] => 0
) [4] => Array
(
[period] => 201307
[out_count] => 251
) [5] => Array
(
[period] => 201308
[out_count] => 0
) [6] => Array
(
[period] => 201309
[out_count] => 807
))
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 < $r['period']) {
$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
))