douqian2334 2017-09-23 13:59
浏览 138
已采纳

在php中为数组添加缺少的日期索引

I have the following array from a count query:

array = (
  0=>array(
    'date'=>2017-09-01,
    'total'=>4
  ),
  1=>array(
    'date'=>2017-09-03,
    'total'=>6
  ),
  # and so on..
);

I want to fill the date even if my query has no records on that date! How can I add the missing date index to the array. The dates are to be sequential. so I want the following output:

array = (
  0=>array(
    'date'=>2017-09-01,
    'total'=>4
  ),
  1=>array(
    'date'=>2017-09-02,
    'total'=>0
  )
  2=>array(
    'date'=>2017-09-03,
    'total'=>6
  ),
  • 写回答

3条回答 默认 最新

  • drny20290570 2017-09-23 14:49
    关注

    Extended solution with DateTime object, array_map and range functions:

    $arr = [
            ['date' => '2017-09-01', 'total' => 4],
            ['date' => '2017-09-07', 'total' => 6],
            ['date' => '2017-09-09', 'total' => 7]
    ];
    
    $result = [];
    foreach ($arr as $k => $item) {
        $d = new DateTime($item['date']);
        $result[] = $item;
        if (isset($arr[$k+1])) {
            $diff = (new DateTime($arr[$k+1]['date']))->diff($d)->days;
            if ($diff > 1) {
                $result = array_merge($result , array_map(function($v) use($d){
                    $d_copy = clone $d;
                    return [
                        'date' => $d_copy->add(new DateInterval('P' . $v. 'D'))->format('Y-m-d'),
                        'total' => 0
                    ];
                }, range(1, $diff-1)));
            }
        }
    }
    
    print_r($result);
    

    The output:

    Array
    (
        [0] => Array
            (
                [date] => 2017-09-01
                [total] => 4
            )
    
        [1] => Array
            (
                [date] => 2017-09-02
                [total] => 0
            )
    
        [2] => Array
            (
                [date] => 2017-09-03
                [total] => 0
            )
    
        [3] => Array
            (
                [date] => 2017-09-04
                [total] => 0
            )
    
        [4] => Array
            (
                [date] => 2017-09-05
                [total] => 0
            )
    
        [5] => Array
            (
                [date] => 2017-09-06
                [total] => 0
            )
    
        [6] => Array
            (
                [date] => 2017-09-07
                [total] => 6
            )
    
        [7] => Array
            (
                [date] => 2017-09-08
                [total] => 0
            )
    
        [8] => Array
            (
                [date] => 2017-09-09
                [total] => 7
            )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图