Method of php Reading and Writing json File and Modifying json

  • 2021-09-12 00:32:49
  • OfStack

The example is as follows:


//  Append the file written to the user name 
$code="001";// Dynamic data 
    $json_string = file_get_contents("text.json");//  Read data from a file to PHP Variable 
    $data = json_decode($json_string,true);//  Put JSON String to PHP Array 
    $data[$code]=array("a"=>"as","b"=>"bs","c"=>"cs");
    $json_strings = json_encode($data);
    file_put_contents("text.json",$json_strings);// Write 
// Modify 
 $json_string = file_get_contents("text.json");//  Read data from a file to PHP Variable 
    $data = json_decode($json_string,true);//  Put JSON String to PHP Array 
    $data["001"]["a"]="aas";
    $json_strings = json_encode($data);
    file_put_contents("text.json",$json_strings);// Write 

Related articles: