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));
txt, and how to insert the number automaticlly increase?numberproperty should not exist. The subobjects already have a natural ordering, so remove the redundant and hard-to-maintainnumberfield and lose nothing.