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