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 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?