我正在用anycharts构建一个仪表板。 我从JSON格式的mysql数据库中获取数据。 查询示例:
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,... < / p>
这可能吗?
由于
答案 0 :(得分: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)
我希望它对你有所帮助!