Json 编码格式错误
Json encode in the Wrong Format
我正在尝试对我网站的访问情况进行一些统计。我知道我可以使用 Google 分析(我会这样做),但我想尝试自己做一些事情,这是学习它的好方法。
问题:
我 select 在我的数据库中日期并将它们排序以适合本周。之后,我想将它们添加到 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);
我的理解是你想在 visits 中将相同的日期分组,如果我理解正确的话这里有一个解决方案。
在将其更改为json字符串之前,添加一个for more循环以生成格式php数组。
$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
将关联转换为索引
我正在尝试对我网站的访问情况进行一些统计。我知道我可以使用 Google 分析(我会这样做),但我想尝试自己做一些事情,这是学习它的好方法。
问题: 我 select 在我的数据库中日期并将它们排序以适合本周。之后,我想将它们添加到 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);
我的理解是你想在 visits 中将相同的日期分组,如果我理解正确的话这里有一个解决方案。
在将其更改为json字符串之前,添加一个for more循环以生成格式php数组。
$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
将关联转换为索引