0

我正在使用任何图表构建仪表板。我从 mysql 数据库中以 JSON 格式获取数据。查询示例:

SELECT DATE( data_inscricao ) AS name, COUNT( id ) AS value FROM utilizador GROUP BY name

所以,我得到了按注册日期分组的所有注册用户的计数。

示例结果:

[{name: "2016-01-22",value: "7"}, {name: "2016-01-25", value: "3"}, {name: "2016-01-26", value: "1"}, {name: "2016-01-27", value: "2"}, {name: "2016-02-02", value: "1"}, ...

我想在没有任何注册的日期显示 0(零),例如 2016-01-23、2016-01-24、2016-01-28、...

那可能吗?

谢谢

4

1 回答 1

0

首先,您需要定义日期的最小值和最大值。然后,您可以生成包含期间每天的数组,并将其与数据库中的值合并。在您的情况下,我们有2016-01-22-2016-02-02期间:

<?php
$begin = new DateTime( '2016-01-22' );
$end = new DateTime( '2016-02-02' );

$allDays = [];
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ) {
    $allDays[$dt->format('Y-m-d')] = '0';
}
foreach ($array as $key => $dayFromDB) {
    $allDays[$dayFromDB->name] = $dayFromDB->value;
}
var_dump($allDays);

$array是您的数据库数组。在此之后你会得到类似的东西:

array (size=12)
'2016-01-22' => string '7' (length=1)
'2016-01-23' => string '0' (length=1)
'2016-01-24' => string '0' (length=1)
'2016-01-25' => string '3' (length=1)
'2016-01-26' => string '1' (length=1)
'2016-01-27' => string '2' (length=1)
'2016-01-28' => string '0' (length=1)
'2016-01-29' => string '0' (length=1)
'2016-01-30' => string '0' (length=1)
'2016-01-31' => string '0' (length=1)
'2016-02-01' => string '0' (length=1)
'2016-02-02' => string '1' (length=1)

我希望它对你有帮助!

于 2017-09-08T19:28:03.757 回答