title: How to update (overwrite) a json file using php
body: <pre><code>[
    {
       "id"  : 1,
      "name"  : "levin",
      "description" : "some desc",
      "size"  : "100KG",
       "actions" : {
                   "walking" : true,
                    "eating" : true
                    }
    },
    {
      "id"  : 2,
      "name"  : "clara",
      "description" : "some desc",
      "size"  : "2000KG",
       "actions" : {
                   "walking" : false,
                    "eating" : true
                    }
  
    }
  ]
  </code></pre>
  
  <p>This is my person.json file. I like to update(overwrite) existing values. Am not found any useful questions regarding this. </p>
  
  <p>i have a name "levin" i like to overwrite it to empty or "---". But its should only based through "id" .Following is my php code but its not working :(</p>
  
  <pre><code>public function api_put()
      {
           //print($this-&gt;put('id')); am getting here 2 value from other page 
          //print($this-&gt;put('action'));.
  
          if($this-&gt;put('action') == "remove"){
              $file = json_decode(file_get_contents("assets/json/person.json"));
              $new_val = array();
              $i = 0 ;
              foreach ($file as $key =&gt; $value) {
                  if((string)$value-&gt;id == $this-&gt;put('id')) {
                      $data[] = (string)$value-&gt;name="--";(string)$value-&gt;description="--";
                      $new_val[$i] =  $data;
                      $i++;
                  }
  
              }
              file_put_contents('assets/json/person.json', json_encode($new_val));
              $message = array('id' =&gt; $this-&gt;put('id'), 'message' =&gt; 'Successfully updated!!');
  
              $this-&gt;response($message, 200); 
          }
      }
  </code></pre>
  
  <p>How can i overwrite a json value depend upon a particular id and with out change all other id data. am using codeigniter REST api. thanks in advance</p>