Php 如何向JSON文件添加数据

Php 如何向JSON文件添加数据,php,Php,我希望能够通过使用PHP的在线门户添加到JSON文件中 我试图用下面的代码来实现这一点,但我不熟悉PHP,因此无法进一步了解 <?php //Get the data from the url $name = $_GET['name']; $email = $_GET['email']; // read json file $data = file_get_contents('example.json'); // decode json $json_arr = json_decode

我希望能够通过使用PHP的在线门户添加到JSON文件中

我试图用下面的代码来实现这一点,但我不熟悉PHP,因此无法进一步了解

<?php

//Get the data from the url
$name = $_GET['name'];
$email = $_GET['email'];

// read json file
$data = file_get_contents('example.json');

// decode json
$json_arr = json_decode($data, true);

// add data
$json_arr[] = array(4, 'Name'=>$name, 'email'=>$email);

// encode json and save to file
file_put_contents('example.json', json_encode($json_arr));
?> 
这就是整个JSON:

{
  "0": {
    "conditionName": "u",
    "ignoreFollowing": false,
    "emailsThisSession": 0,
    "recieverEmails": [
      "example@g.c"
    ],
    "alertType": 1,
    "emailProperty": 2,
    "triggers": {
      "0": {
        "Property": 2,
        "Value": "zk-reg",
        "ComparisonType": "Equals"
      }
    }
  },
  "1": {
    "conditionName": "k",
    "ignoreFollowing": false,
    "emailsThisSession": 0,
    "recieverEmails": [
      "h@f.com"
    ],
    "alertType": 1,
    "emailProperty": 0,
    "triggers": {
      "0": {
        "Property": 0,
        "Value": "",
        "ComparisonType": "Equals"
      }
    }
  }
}

如果我对你的理解是正确的,你想做一些类似的事情

<?php



 // read json file in this case I just include it in the script for simplicity

$data = '{
  "0": {
    "conditionName": "u",
    "ignoreFollowing": false,
    "emailsThisSession": 0,
    "recieverEmails": [
      "example@g.c"
    ],
    "alertType": 1,
    "emailProperty": 2,
    "triggers": {
      "0": {
        "Property": 2,
        "Value": "zk-reg",
        "ComparisonType": "Equals"
      }
    }
  },
  "1": {
    "conditionName": "k",
    "ignoreFollowing": false,
    "emailsThisSession": 0,
    "recieverEmails": [
      "h@f.com"
    ],
    "alertType": 1,
    "emailProperty": 0,
    "triggers": {
      "0": {
        "Property": 0,
        "Value": "",
        "ComparisonType": "Equals"
      }
    }
  }
}';

// decode json
$json_arr = json_decode($data, true);
echo "Before Count " . count($json_arr);
// add data
//put the array of variables you want to add into its own var, add each variable you want, I've done the first two
$rowToAdd = array("conditionName" => "y",  "ignoreFollowing" => false, "emailsThisSession" => 800, "recieverEmails" => array("h@f.com"));

//use array_push to add to the current array rather over-writing it 
array_push($json_arr , $rowToAdd);
echo "After Count " . count($json_arr);
// encode json and save to file
file_put_contents('example.json', json_encode($json_arr));
?> 
试试看:

//$data is data you wanto add.
$json_files = file_get_contents('example.json');
$tempArray = json_decode($json_files);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('example.json', $jsonData);

我觉得不错。你的代码有什么问题吗?在PHP中,我不知道如何以那种格式插入所有数据。你编辑了文章。你用array_push($array,$value1,$value2)将数据添加到PHP数组中;我认为您要做的是将数组设置为数组(4,'Name'=>$Name,'email'=>$email);在解码之后。而是解码json,然后使用array_push将其添加到当前数组中。我希望从表单中获取所有数据,然后以以下格式将其添加到json中:
“0”:{“conditionName”:“u”,“ignoreFollowing”:false,“EmailSthisession”:0,“ReceiveEmails”:[“example@g.c“],“alertType”:1,“emailProperty”:2,“触发器”:{“0”:{“Property”:2,“Value”:“zk reg”,“ComparisonType”:“Equals”}},
我明白了,给我几分钟时间,我会更新答案。感谢@Pheonix2105对我说得不太清楚表示歉意。看一看,确保按f9运行它,这是你想要的吗?你会注意到我做了一个var转储,而不是写入json文件,因为你显然不能在phpfiddle中使用,但你可以在脚本中使用。@radarYup谢谢什么会让我开始将其余的变量添加到。
//$data is data you wanto add.
$json_files = file_get_contents('example.json');
$tempArray = json_decode($json_files);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('example.json', $jsonData);