Php 如何使用带有子数组的数组和json_编码来加载部分数据

Php 如何使用带有子数组的数组和json_编码来加载部分数据,php,jsonencoder,Php,Jsonencoder,我在数组中有以下数据,其中包含两个子数组0和1。我试图通过json_encode每次仅加载子数组数据0或1: 我的网站中的php代码: <?php $dataPoints_temp = array(array(),array()); $row = 1; if (($handle = fopen($latestFile, "r")) !== FALSE) { $data = array(); while (

我在数组中有以下数据,其中包含两个子数组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]));
            }
        }
    }
?>
但是这在浏览器的控制台中给了我很多错误,尽管
$id
已经定义并且目前设置为
0

请尝试@Wimanicesir的建议,我将所有数据存储在一个php数组中,并使用
json\u 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, … }
...
还有两次尝试:

  • $client
    值与php的预期值相同
    json\u encode
  • var$test1=[{“hum”:38.31,“id”:“51df”,“pm1m0”:1,“pm2m5”:1,“pres”:1008.25,“temp”:22.24}]

  • $client value将数据表示为字符串:
  • 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);
    
    在上述两种情况下,只有在第二点,
    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);
        }
    }
    
    var$rawData=;
    var$json=json.stringify($rawData);
    var$dataPoints=JSON.parse($JSON);
    const clients=[…新集合($dataPoints.map(data=>data.id))];//查找所有唯一的客户机id,它们是示例数据“51df”和“51ff”中的两个
    var$dataPoints1=[];
    var$dataPoints2=[];
    var$dataPointsHum=[];
    var$dataPointsP2m5=[];
    var$dataPointsP1m0=[];
    var$clientId=0;//我们需要客户端0的数据
    对于(i=0;i<$dataPoints.length;i++){
    if(clients[$clientId]=$dataPoints[i].id){//仅基于$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);
    }
    }
    
    上面的代码在两个客户机的数据样本上进行了测试,似乎运行良好

    我该怎么做


    感谢您使用JS变量访问PHP中的某些内容。这不能这样做。也不建议像那样将JS和PHP结合起来。要解决当前问题,请在PHP中创建一个id为的变量。嗯,我想我的问题是,我无法再次启动
    window.onload
    ,因为网站已经加载。因为client1的图形应该显示在tab1中,而client2的图形应该显示在tab2中,切换选项卡不会再次运行
    window.onload
    !如果它在同一个页面上,那么将完整的数组从PHP加载到JS中。然后使用json.parse()访问它。换言之,使用PHP主要用于传递数据,并使用JS访问和解析数据。我已根据您的建议尝试更新了我的问题,但我发现
    JSON.parse出现错误。我做错了什么?
    JSON.parse
    确实需要一个
    字符串作为输入。你知道JSON代表什么吗?它代表JavaScript对象表示法。在PHP中调用
    json\u encode
    时,您正在创建一个
    字符串
    ,该字符串将传递为实际有效的JavaScript语法。所以当你做
    var$rawData=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
    
    var $test2 = JSON.parse($test1);
    console.log($test2);
    
    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);
        }
    }