用php补全数组中的日期

hxl5u 2013-12-27 09:27:44
//如图如果遇到这样的数组,如何去补齐数组中每一次的period值 缺少的月份让它补齐 实现每一个大数组有12个月份的数组(因为有时候没有前半年 或后半年的月份),每一次的[period]值 补齐为从(201301~201312)的结构。

就是如从201305月开始那就补齐 01~04。如果从10月开始 补齐 01~09. 按月排序每个二维数组。

Array
(
[0] => Array
(
[period] => 201303
[out_count] => 383
)

[1] => Array
(
[period] => 201304
[out_count] => 43
)

[2] => Array
(
[period] => 201306
[out_count] => 2859
)

[3] => Array
(
[period] => 201307
[out_count] => 251
)

[4] => Array
(
[period] => 201309
[out_count] => 807
)

)


//想得到
Array
(
[0] => Array
(
[period] => 201301
[out_count] => 0
)

[1] => Array
(
[period] => 201302
[out_count] => 0
)

[2] => Array
(
[period] => 201303
[out_count] => 0
)

[3] => Array
(
[period] => 201304
[out_count] => 0
)

[4] => Array
(
[period] => 201305
[out_count] => 0
)

[5] => Array
(
[period] => 201306
[out_count] => 0
)

[6] => Array
(
[period] => 201307
[out_count] => 0
)

[7] => Array
(
[period] => 201308
[out_count] => 0
)
[8] => Array
(
[period] => 201309
[out_count] => 0
)

[9] => Array
(
[period] => 201310
[out_count] => 0
)

[10] => Array
(
[period] => 201311
[out_count] => 0
)

[11] => Array
(
[period] => 201312
[out_count] => 0
)


)
...全文
508 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxl5u 2014-01-02
  • 打赏
  • 举报
回复
好的 谢谢各位··换了个思路 我把时间改成在sql语句中实现了 按每个月group by date
xuzuning 2013-12-28
  • 打赏
  • 举报
回复
array_unshift($a,array('period' => 201301, 'out_count' => 0)); //这样就添加了起点
hxl5u 2013-12-28
  • 打赏
  • 举报
回复
[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月都补的话···能实现么
xuzuning 2013-12-28
  • 打赏
  • 举报
回复
终点应放在原始数组中,不然算法不可能自作主张的添加数据
$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
        )

)

hxl5u 2013-12-28
  • 打赏
  • 举报
回复
引用 8 楼 xuzuning 的回复:
$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"));
}

老大这个真不错 我试试补齐12个月·· 谢谢谢谢
xuzuning 2013-12-28
  • 打赏
  • 举报
回复
$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
        )

)

hxl5u 2013-12-27
  • 打赏
  • 举报
回复
引用 6 楼 jordan102 的回复:
那你直接构建一个不就行了?
$m=range(201301,201312);
foreach($m as $v){
    $arr[]=array('period' =>$v,'out_count' =>0);
}
print_r($arr);
额因为时间是变化的 ,不同年月份 ,就是补齐那些缺少的月份,又不被覆盖 已有的
一起混吧 2013-12-27
  • 打赏
  • 举报
回复
那你直接构建一个不就行了?
$m=range(201301,201312);
foreach($m as $v){
    $arr[]=array('period' =>$v,'out_count' =>0);
}
print_r($arr);
hxl5u 2013-12-27
  • 打赏
  • 举报
回复
引用 2 楼 hbgth 的回复:
判断是否为上一字符串+1
我试一下看哈 谢谢哥们
hxl5u 2013-12-27
  • 打赏
  • 举报
回复
引用 3 楼 jordan102 的回复:
补齐之后每个out_count都为0 了?
是的 补齐之后是个空的 但是要这个日期
一起混吧 2013-12-27
  • 打赏
  • 举报
回复
补齐之后每个out_count都为0 了?
hbgth 2013-12-27
  • 打赏
  • 举报
回复
判断是否为上一字符串+1
hbgth 2013-12-27
  • 打赏
  • 举报
回复
我的想法:首先Count(array())的个数,如果小于12,则从第一个字符串开始右截取其2位(比如201301的01),看其是否==01,如果不是,则自动添加从201301到该字符串,如果是,则同时右截取下一个字符串,并判断是否为上一字符串 1,如果是,则自动继续后面的,不是,则补齐中间空缺的字符串。于此类推。 中间可能需要用到字符串和数字转换的函数。

20,397

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧