配列の内容を含む JSON ファイルを書き込もうとする次の php スクリプトがあります。
$name = "TEST";
$type = "TEST";
$price = "TEST";
$upload_info = array();
$upload_info = array('name'=>$name,'type'=>$type,'price'=>$price);
$temp_array = json_decode(file_get_contents('upload.json'));
array_push($temp_array, $upload_info);
file_put_contents('items.json', json_encode($temp_array));
$json = json_encode($upload_info);
$file = "items.json";
file_put_contents($file, $json);
これにより、次の行が items.json に追加されます。
{"name":"TEST","type":"TEST","price":"TEST"}
また、次のエラーが発生します。
PHP Warning: array_push() expects parameter 1 to be array, null given in /var/www/html/list/add.php on line 13
私が理想的にやりたいことは、次のような json ファイルの特定の部分にスクリプトを追加することです。
{
"GC": [
{ "Name":"Thing" , "Price":"??" , "Link":"www.google.com" },
{ "Name":"Thing" , "Price":"??" , "Link":"www.google.com" }
]
"0": [
]
"10": [
]
"20": [
]
"30": [
]
"40": [
]
"50": [
]
"60": [
]
"70": [
]
"80": [
]
"90": [
]
"100": [
]
"200": [
]
"300": [
]
"400": [
]
"500": [
]
}
これらの配列のそれぞれは、add.php でアイテムが追加された後、(GC 配列と同様に) アイテムを保持します。これは可能ですか?もしそうなら、どうすればいいですか?
エントリを追加したい json の特定の部分を選択することは可能ですか? これを行う簡単な方法はありますか?データベースを使用せずにデータを保存するソリューションを探しています。