How to read and write json files in php and modify json methods

小云云
Release: 2023-03-20 22:44:01
Original
3347 people have browsed it

This article mainly shares with you a method of reading and writing json files and modifying json in PHP. It has a good reference value and I hope it can help everyone.

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);//写入
Copy after login

Related recommendations:

How to use python to read and write json files

javascript read and write json example_javascript skills

js read and write json file example code_javascript skills

The above is the detailed content of How to read and write json files in php and modify json methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!