我在数组中有以下数据,其中包含两个子数组 0 和 1。我试图每次通过 json_encode 仅加载子数组数据 0 或 1:
我网站上的 php 代码:
<?php
$dataPoints_temp = array(array(),array());
$row = 1;
if (($handle = fopen($latestFile, "r")) !== FALSE) {
$data = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
list($id, $time, $room, $pressure, $humidity, $pm2m5, $pm1m0) = $data;
if ($id == "client1") {
array_push($dataPoints_temp[0], array("x" => $data[0], "y" => $data[2]));
} else if ($id == "client2") {
array_push($dataPoints_temp[1], array("x" => $data[0], "y" => $data[2]));
}
}
}
?>
我网站中的 javascript 现在执行以下操作:
<script>
window.onload = function() {
var updateInterval = 1500;
var $id = 0;
var $dataPoints1 = <?php echo json_encode($dataPoints_temp[0], JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK); ?>;
...
}
</script>
这行得通,我确实让我的图表工作了。我现在正在尝试的想法是使$dataPoints_temp[0]
能够选择 0 或 1 作为图形的数据点更通用。
var $dataPoints1 = <?php echo json_encode($dataPoints_temp[$id], JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK); ?>;
但这在浏览器的控制台中给了我很多错误,尽管$id
已定义并且暂时设置为0
.
尝试@Wimanicesir 的建议,我将所有数据存储在一个 php 数组中,并使用 将json_encode
其存储到 javascript 数组中,然后尝试JSON.parse
用于该数组,但出现错误,请参阅。
客户资料:
(5) […]
0: {…}
hum: 38.31
id: "51df"
pm1m0: 1
pm2m5: 1
pres: 1008.25
temp: 22.24
<prototype>: Object { … }
1: Object { id: "51df", temp: 22.24, pres: 1008.26, … }
...
见代码:
var $rawData = <?php echo json_encode($client, JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK); ?>;
var $obj = JSON.parse($rawData);
控制台错误:
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
再试两次:
$client
值与 php 预期的一样json_encode
:
var $test1 = [{"hum": 38.31, "id": "51df", "pm1m0": 1, "pm2m5": 1, "pres": 1008.25, "temp": 22.24}];
- $client 值将数据表示为字符串:
var $test1 = '{"hum": 38.31, "id": "51df", "pm1m0": 1, "pm2m5": 1, "pres": 1008.25, "temp": 22.24}';
其余测试代码:
var $test2 = JSON.parse($test1);
console.log($test2);
以上两种情况只有第2点JSON.parse
是成功的!这是否意味着JSON.parse
需要一个字符串作为输入?
进一步调查后更新:
var $rawData = <?php echo json_encode($client, JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK); ?>;
var $json = JSON.stringify($rawData);
var $dataPoints = JSON.parse($json);
const clients = [... new Set($dataPoints.map(data => data.id))]; // find all unique clients id's, which are two in the sample data '51df' & '51ff'
var $dataPoints1 = [];
var $dataPoints2 = [];
var $dataPointsHum = [];
var $dataPointsP2m5 = [];
var $dataPointsP1m0 = [];
var $clientId = 0; // we want the data for client 0
for (i = 0; i < $dataPoints.length; i++) {
if (clients[$clientId] == $dataPoints[i].id) { // load data only for client based on $clientId
var tuple1 = {x: $dataPoints[i].id, y: $dataPoints[i].temp};
$dataPoints1.push(tuple1);
var tuple2 = {x: $dataPoints[i].id, y: $dataPoints[i].pres};
$dataPoints2.push(tuple2);
var tuple_hum = {x: $dataPoints[i].id, y: $dataPoints[i].hum};
$dataPointsHum.push(tuple_hum);
var tuple_p2m5 = {x: $dataPoints[i].id, y: $dataPoints[i].pm2m5};
$dataPointsP2m5.push(tuple_p2m5);
var tuple_p1m0 = {x: $dataPoints[i].id, y: $dataPoints[i].pm1m0};
$dataPointsP1m0.push(tuple_p1m0);
}
}
上面的代码似乎在两个客户端的数据样本上运行良好。
我怎样才能做到这一点?
谢谢