[英]Adding elements to an array inside of JSON
I have some JSON that looks like this: 我有一些看起来像这样的JSON:
{
people: []
}
My Goal at the moment is to simply add elements to the array of people. 我目前的目标是将元素简单地添加到人群中。 But in the end I also would like to be able to remove the elements again.
但最后,我也希望能够再次删除这些元素。
I have tried something like: 我已经尝试过类似的东西:
<?
$file = "test.json";
$json = file_get_contents($file);
$get = json_decode($json);
array_push($get->people, $_GET['person']);
file_put_contents($file, json_encode($get));
?>
However, this does not work. 但是,这不起作用。 I'm pretty sure there is an easier solution for this.
我敢肯定,有一个更简单的解决方案。 Thanks for any help.
谢谢你的帮助。
You have invalid JSON . 您的JSON无效 。 Keys must be quoted .
键必须加引号 。
{"people": []}
Otherwise, your code should work. 否则,您的代码应该可以工作。 Although I would recommend not using PHP short open tags and filtering user input .
尽管我建议不要使用PHP短开放标签并过滤用户输入 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.