Php 向从JSON读取的数组添加新数据

Php 向从JSON读取的数组添加新数据,php,arrays,json,Php,Arrays,Json,我有以下JSON: [ { "unit": "1", "measurings": [ { "timestamp": "1576788585", "weight":35, "temp": 35.3 }, { "timestamp": "1576788595", "weight":35, "temp": 35.3 } ] },

我有以下JSON:

[
  {
    "unit": "1",
    "measurings": [
      {
        "timestamp": "1576788585",
        "weight":35,
        "temp": 35.3
      },
      {
        "timestamp": "1576788595",
        "weight":35,
        "temp": 35.3
      }
    ]
  },
  {
    "unit": "2",
    "measurings": [
      {
        "timestamp": "1576788604",
        "weight":47,
        "temp": 35.3
      },
      {
        "timestamp": "1576788610",
        "weight":35,
        "temp": 35.3
      }
    ]
  },
  {
    "unit": "3",
    "measurings": [
      {
        "timestamp": "1576788604",
        "weight":36,
        "temp": 35.3
      },
      {
        "timestamp": "1576788610",
        "weight":34,
        "temp": 35.3
      }
    ]
  },
  {
    "unit": "4",
    "measurings": [
      {
        "timestamp": "1576788684",
        "weight":32,
        "temp": 35.3
      },
      {
        "timestamp": "1576788690",
        "weight":37,
        "temp": 34.3
      }
    ]
  }
]

现在我想将数据添加到现有(可能还有新的)单元。我想收集测量数据,因此需要将新数据推送到JSON文件中。我考虑将JSON解码成一个数组,并向其中添加
$\u POST
数据。 我的解码数组如下所示:


Array
(
    [0] => Array
        (
            [unit] => 1
            [measurings] => Array
                (
                    [0] => Array
                        (
                            [timestamp] => 1576788585
                            [weight] => 35
                            [temp] => 35.3
                        )

                    [1] => Array
                        (
                            [timestamp] => 1576788595
                            [weight] => 35
                            [temp] => 35.3
                        )

                )

        )

    [1] => Array
        (
            [unit] => 2
            [measurings] => Array
                (
                    [0] => Array
                        (
                            [timestamp] => 1576788604
                            [weight] => 47
                            [temp] => 35.3
                        )

                    [1] => Array
                        (
                            [timestamp] => 1576788610
                            [weight] => 35
                            [temp] => 35.3
                        )

                )

        )

    [2] => Array
        (
            [unit] => 3
            [measurings] => Array
                (
                    [0] => Array
                        (
                            [timestamp] => 1576788604
                            [weight] => 36
                            [temp] => 35.3
                        )

                    [1] => Array
                        (
                            [timestamp] => 1576788610
                            [weight] => 34
                            [temp] => 35.3
                        )

                )

        )

    [3] => Array
        (
            [unit] => 4
            [measurings] => Array
                (
                    [0] => Array
                        (
                            [timestamp] => 1576788684
                            [weight] => 32
                            [temp] => 35.3
                        )

                    [1] => Array
                        (
                            [timestamp] => 1576788690
                            [weight] => 37
                            [temp] => 34.3
                        )

                )

        )

)

到目前为止,我的PHP代码如下所示:

<?php
$time = time();
//$tempF = $_POST["temp"];
//$weight = $_POST["weight"];
//$beute = $_POST["beute"];
$temp = "999";
$weight = "1111";
$unit = "1";
$file = 'measuringData.json';

$data = json_decode(file_get_contents($file), 1);


echo "<pre>";
print_r($data);
echo "</pre>";

//file_put_contents($file, $data);  

如果单元1存在,并且您希望将其添加到单元匹配的第一个值中
$unit
,您可以使用循环将其添加到第一个匹配中,然后使用:


Php演示

您可以使用下一个函数:

可作为
adddatabynit($data,$unitData,$unitId)使用

$item = [
    "timestamp" => $time,
    "weight" => $weight,
    "temp" => $temp
];
foreach ($data as $key => $value) {
    if ($value["unit"] === $unit) {
        $data[$key]["measurings"][] = $item;
        break;
    }
}
function addDataByUnit(&$data, $unitData, $unitId){  

        // if unit exists  in the main data set
        if (in_array($unitId,array_column($data,'unit'))){    
            foreach($data as $ind => $info){
                if ($info['unit'] == $unitId) $data[$ind]['measurings'][] = $unitData;   
            } 
        } else {   // if it's a new unit -> add to set
            $data[] = ['unit' => $unitId, 'measurings' => $unitData];
        }

}