我有这个.json文件:
[ { "id": 1, "title": "Ben\\'s First Blog Post", "content": "This is the content" }, { "id": 2, "title": "Ben\\'s Second Blog Post", "content": "This is the content" } ]
这是我的PHP代码:
<?php $data[] = $_POST['data']; $fp = fopen('results.json', 'a'); fwrite($fp, json_encode($data)); fclose($fp);
问题是,我不确定如何实现它。每次提交表单时,我都会在上面调用此代码,因此我需要ID递增,并且还要使用[和保持有效的JSON结构{,这可能吗?
[
{
$data[] = $_POST['data']; $inp = file_get_contents('results.json'); $tempArray = json_decode($inp); array_push($tempArray, $data); $jsonData = json_encode($tempArray); file_put_contents('results.json', $jsonData);