Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数组中添加缺少的月份_Php_Mysql_Arrays - Fatal编程技术网

Php 在数组中添加缺少的月份

Php 在数组中添加缺少的月份,php,mysql,arrays,Php,Mysql,Arrays,我有一个SQL表,从中返回月份和视图。 查询: 编辑:上述函数返回 Array ( [0] => stdClass Object ( [Month] => 2018-August [value] => 21 ) [1] => stdClass Object ( [Month] => 2018-October [value] => 3 )

我有一个SQL表,从中返回月份和视图。 查询:

编辑:上述函数返回

    Array
 (
    [0] => stdClass Object
        (
        [Month] => 2018-August
        [value] => 21
 )

    [1] => stdClass Object
 (
        [Month] => 2018-October
        [value] => 3
 )

    [2] => stdClass Object
 (
        [Month] => 2018-September
        [value] => 3
 )

    [3] => stdClass Object
 (
        [Month] => 2019-January
        [value] => 1
 )

    [4] => stdClass Object
 (
        [Month] => 2019-October
        [value] => 1
 )
PHP

简化形式:

月份:2018年8月:浏览量:19
月份:2018年10月:浏览量:1
月份:2019年1月:浏览量:2
月份:2019年2月:浏览量:8
月份:2019年7月:浏览量:14

我想要的是插入视图为0的缺少月份 如果结果中不存在月份:

月份:2018年8月:浏览量:19
月份:2018年9月:视图:0-表中没有此项。我已分配给0
月份:2018年10月:浏览量:1
月份:2018年11月:浏览量:0-这不在表中。我已分配给0
月份:2018年12月:浏览量:0-此项不在表中。我已分配给0

我遵循这个答案,这是我想要的解决方案,但需要多年的时间。如果您看到这个答案,您将不会发现通过在之前创建值为0的months数组并稍后使用数据库值覆盖它来处理年份是很容易的

链接:

您可以使用for循环

$data=$this->users->chartData();
$keys=array();
$values=array();
for ($cnt = 0; $cnt<12; $cnt++){
  if (isset($data[$cnt])){
    $keys[$cnt] = $cnt;
    $values[$cnt] = $data[$cnt]['value'];
  } else {
    $keys[$cnt] = $cnt;
    $values[$cnt] = 0;
  }
}
$data=$this->users->chartData();
$keys=array();
$values=array();
对于($cnt=0;$cnt可能是这样?
我循环从键的开始到键的结束作为strotime

我使用array_search查看是否在值中找到它,如果不添加零值,则添加值

$keys = ["2018-August","2018-October","2018-September","2019-January","2019-October"];
$values = [21,3,3,1,1];

For($i= strtotime($keys[0]); $i<=strtotime(end($keys)); $i+=86400*31){
    $search = array_search(date("Y-F", $i), $keys);
    if($search !== false){
        $new[$keys[$search]] = $values[$search];
    }else{
        $new[date("Y-F", $i)] =0;
    }
}
$new[end($keys)] = end($values); //add last item since I loop between the dates
Var_dump($new);

只需将计数器添加到循环中。当行结果与计数器不匹配时,打印0。感谢您的回答:)请检查上面的编辑。我应该配什么?如果钥匙不在那里,我就得插一把。你能提供一个解决方案吗?这对我很有帮助!感谢您的回复,请检查以上编辑。实际上,返回的结果不是同一年。这一年也将改变。您发布的答案并没有给我在索引中添加月份的解决方案,应该在索引中添加月份,就像应该有一个键“2018年9月”,该键后面的值为零august@MuhammadOsama循环仍然有效。。但是你应该从你需要的那一个月开始就开始考虑结果。。从9到12,然后从1到8开始。上面的解决方案是打印上面的结果:((屏幕截图链接)附加
$data=$this->users->chartData();
$keys=array();
$values=array();
for ($cnt = 0; $cnt<12; $cnt++){
  if (isset($data[$cnt])){
    $keys[$cnt] = $cnt;
    $values[$cnt] = $data[$cnt]['value'];
  } else {
    $keys[$cnt] = $cnt;
    $values[$cnt] = 0;
  }
}
$keys = ["2018-August","2018-October","2018-September","2019-January","2019-October"];
$values = [21,3,3,1,1];

For($i= strtotime($keys[0]); $i<=strtotime(end($keys)); $i+=86400*31){
    $search = array_search(date("Y-F", $i), $keys);
    if($search !== false){
        $new[$keys[$search]] = $values[$search];
    }else{
        $new[date("Y-F", $i)] =0;
    }
}
$new[end($keys)] = end($values); //add last item since I loop between the dates
Var_dump($new);
array(15) {
  ["2018-August"]=>
  int(21)
  ["2018-September"]=>
  int(3)
  ["2018-October"]=>
  int(3)
  ["2018-November"]=>
  int(0)
  ["2018-December"]=>
  int(0)
  ["2019-January"]=>
  int(1)
  ["2019-February"]=>
  int(0)
  ["2019-March"]=>
  int(0)
  ["2019-April"]=>
  int(0)
  ["2019-May"]=>
  int(0)
  ["2019-June"]=>
  int(0)
  ["2019-July"]=>
  int(0)
  ["2019-August"]=>
  int(0)
  ["2019-September"]=>
  int(0)
  ["2019-October"]=>
  int(1)
}