私は次のJSONファイルをサーバーに保存しています。私はこのJSONファイルに別のオブジェクトを追加するためにPHPスクリプトを使用しています。PHPを使用してJSONオブジェクトを.jsonファイルに挿入
testjson1.json:
{
"feed":
[
{
"id": "1",
"name": "Ram",
"status": "Very good restaurant!! water tastes soo good :)"
}
]
}
次のように私はオブジェクトを追加するために使用するPHPは次のとおりです。
<?php
$data=array("2","new","not so good");
$inp = file_get_contents('testjson1.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('testjson1.json', $jsonData);
?>
しかし、PHPファイルは述べているエラーを示しています。
を警告:array_push()は、パラメータ1が配列になることを期待しています。オブジェクトは/home/u160481344/public_html/jsonfinder.php 5行目に指定されています
とjsonファイルが1行に崩壊します。どうすればこの問題を解決できますか? documentation 1として