Home >Backend Development >PHP Tutorial >Detailed explanation of the steps to read, write and modify json files in php

Detailed explanation of the steps to read, write and modify json files in php

php中世界最好的语言
php中世界最好的语言Original
2018-05-18 14:05:432123browse

This time I will bring you a detailed explanation of the steps for reading, writing and modifying json files in PHP. What are the precautions for reading, writing and modifying JSON files in PHP? The following is a practical case, let's take a look.

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);//写入
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!

Recommended reading:

Detailed explanation of the steps to implement single sign-in with Yii2

A brief discussion on the detailed explanation of the use of pack and unpack in PHP

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

Statement:
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