Home>Article>Backend Development> How to modify json data format in php

How to modify json data format in php

醉折花枝作酒筹
醉折花枝作酒筹 Original
2021-04-19 18:28:41 2047browse

Method: First use the "json_decode(string,true)" statement to convert JSON format into array format; then use the "array[element]="value"" statement to modify the data; finally use "json_encode( Array)" statement to convert the modified array into json format.

How to modify json data format in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

// 追加写入用户名下文件 $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);//写入

Recommended: "Summary of PHP interview questions in 2021 (collection)》《php video tutorial

The above is the detailed content of How to modify json data format 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