How can I add new data to a JSON structure?
Here is the JSON in json.txt:
[
{"name":"foo","number":"1"},
{"name":"bar","number":"2"},
{"name":"Hello","number":"3"}
]
Now I want to add a new line {"name":"good day","number":"**"}
$file = 'json.txt';
$data = json_decode(file_get_contents($file));
$newdata = array('name'=>'good day', 'number' => '**');// how to add `number` automatic `+1`, make it to `4` with php code?
$data[] = $newdata;
file_put_contents($file, json_encode($data));
In php < 5.3, replace the
$newNumber =statement with: