[英]Json encode in the Wrong Format
我正在尝试对我网站上的访问进行一些统计。 我知道我可以使用 Google Analytics(我会这样做),但我想尝试自己做一些事情,这是学习它的好方法。
问题:我在我的数据库中选择日期并对它们进行排序以适合本周。 之后,我想将它们添加到 json 文件中。 CanvasJS 使用该 json 文件制作图表。 我尝试了一些不同的方法,只是为了让它模拟工作。 但是json数组的格式,并不是CanvasJS想要的。
我需要的:
{ visits:[[2019-02-12, 49,],[2019-02-13,40,],[2019-02-14,46,],[2019-02-15,37,], [2019-02-16,31,],[2019-02-17,38,],[2019-02-18,4,] }
我得到的:
{ "visits":{"2019-02-12":49,"2019-02-13":40,"2019-02-14":46,"2019-02-15":37,"2019-02-16":31,"2019-02-17":38,"2019-02-18":4} }
我的 PHP 脚本:
// Get first and last day of the current week
$first_day = date('Y-m-d', strtotime("- 6 days"));
$last_day = date('Y-m-d', strtotime("+ 1 days "));
// Return all results the past 7 days
$sql = "SELECT date FROM table WHERE date >= '" . $first_day . "' AND date < '" . $last_day . "'";
if($result = $conn->query($sql)){
$response = array();
$visits = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$old_date = $row['date'];
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d', $old_date_timestamp);
// I dont need the keys, but I cant avoid it to
// get it to work....
$visits[] = array(
'date' => $new_date
);
}
// Add sum of Dates
$response['visits'] = array_count_values(array_column($visits, 'date'));
// Save to json File
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
}
$conn->close();
感谢任何能够提供帮助的人。
忽略您可能认为是问题(但不是)的任何与报价相关的问题,似乎您的主要区别在于您想要的...
[[2019-02-12, 49,],...
以及你所拥有的……
{"2019-02-12":49,...
这是因为array_count_values()
创建了一个关联数组,以您的日期作为键。
通过让您的数据库进行分组和计数,而不是在 PHP 中进行,可以大大简化您的问题。 您还可以从使用准备好的语句而不是直接值注入中受益。
// Get first and last day of the current week
$first_day = date('Y-m-d', strtotime("- 6 days"));
$last_day = date('Y-m-d', strtotime("+ 1 days "));
$sql = <<<_SQL
SELECT DATE(`date`), COUNT(1)
FROM `table` WHERE `date` BETWEEN ? AND ?
GROUP BY DATE(`date`)
_SQL;
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss', $first_day, $last_day);
$stmt->execute();
$stmt->bind_result($date, $count);
while ($stmt->fetch()) {
$visits[] = [$date, $count];
}
$response = [ 'visits' => $visits ];
// Save to json File
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
我的理解是你想在访问中将相同的日期与数组分组,如果我理解正确,这里是一个解决方案。
$first_day = date('Y-m-d', strtotime('- 6 days'));
$last_day = date('Y-m-d', strtotime('+ 1 days '));
$sql = "SELECT date FROM table WHERE date >= '" . $first_day . "' AND date < '" . $last_day . "'";
if ($result = $conn->query($sql)) {
$response = [];
$visits = [];
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$old_date = $row['date'];
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d', $old_date_timestamp);
$visits[] = [
'date' => $new_date
];
}
// here the change start
$format = [];
foreach ($visits as $visit) {
$format[$visit['date']][] = $visit['date'];
}
$response['visits'] = array_values($format);
// here the change end
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
}
$conn->close();
date
另一种解决方案$first_day = date('Y-m-d', strtotime('- 6 days'));
$last_day = date('Y-m-d', strtotime('+ 1 days '));
$sql = "SELECT date FROM table WHERE date >= '" . $first_day . "' AND date < '" . $last_day . "'";
if ($result = $conn->query($sql)) {
$response = [];
$visits = [];
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$old_date = $row['date'];
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d', $old_date_timestamp);
$visits[$new_date][] = $new_date; // change here
}
$response['visits'] = array_values($visits); // change here
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
}
$conn->close();
在 PHP 中有两种类型的数组索引和关联,当您将 PHP 数组更改为 json 字符串时,索引数组变为数组,关联数组变为对象。
例子
$indexed = [
0 => 'foo',
1 => 'bar'
];
$associative = [
'one' => 'foo',
'two' => 'bar'
];
var_dump(json_encode($indexed));
// [
// "foo",
// "bar"
// ]
var_dump(json_encode($associative));
// {
// one: "foo",
// two: "bar"
// }
在我的代码中,我使用访问日期作为键,这样相同的日期将进入相同的数组,并且我使用array_values
将关联转换为索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.