さて、私はこれを行うより効率的な方法を見つけました。
Original Answer
// read the file if present
$handle = @fopen($filename, 'r+');
// create the file if needed
if ($handle === null)
{
$handle = fopen($filename, 'w+');
}
if ($handle)
{
// seek to the end
fseek($handle, 0, SEEK_END);
// are we at the end of is the file empty
if (ftell($handle) > 0)
{
// move back a byte
fseek($handle, -1, SEEK_END);
// add the trailing comma
fwrite($handle, ',', 1);
// add the new json string
fwrite($handle, json_encode($event) . ']');
}
else
{
// write the first event inside an array
fwrite($handle, json_encode(array($event)));
}
// close the handle on the file
fclose($handle);
}
アレイに全体JSONファイルを復号することなく。
1)データを配列に入れ、JSONにエンコードするために 'json_encode()'を使います。2)stuffを追加するときは、ファイルの内容を取り出し、デコードして配列を追加してからエンコードしますもう一度それを保存する前に。 – Rizier123
あなたはどう思いますか?どのように改善しますか?ヒント:['json_encode'](http://php.net/manual/de/function.json-encode.php) – Timo
あなたはどんなエラーが出ていますか? –