Home > Article > Backend Development > How to read, write and modify json files in php
This article mainly introduces the methods of reading and writing json files and modifying json in PHP. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
The example is as follows:
// 追加写入用户名下文件 $code="001";//动态数据 $json_string = file_get_contents("text.json");// 从文件中读取数据到PHP变量 $data = json_decode($json_string,true);// 把JSON字符串转成PHP数组 $data[$code]=array("a"=>"as","b"=>"bs","c"=>"cs"); $json_strings = json_encode($data); file_put_contents("text.json",$json_strings);//写入 //修改 $json_string = file_get_contents("text.json");// 从文件中读取数据到PHP变量 $data = json_decode($json_string,true);// 把JSON字符串转成PHP数组 $data["001"]["a"]="aas"; $json_strings = json_encode($data); file_put_contents("text.json",$json_strings);//写入
Related recommendations:
php reads, writes and modifies json files Detailed steps
The above is the detailed content of How to read, write and modify json files in php. For more information, please follow other related articles on the PHP Chinese website!