實(shí)例如下所示:
// 追加寫入用戶名下文件 $code="001";//動態(tài)數(shù)據(jù) $json_string = file_get_contents("text.json");// 從文件中讀取數(shù)據(jù)到PHP變量 $data = json_decode($json_string,true);// 把JSON字符串轉(zhuǎn)成PHP數(shù)組 $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");// 從文件中讀取數(shù)據(jù)到PHP變量 $data = json_decode($json_string,true);// 把JSON字符串轉(zhuǎn)成PHP數(shù)組 $data["001"]["a"]="aas"; $json_strings = json_encode($data); file_put_contents("text.json",$json_strings);//寫入
以上這篇php 讀寫json文件及修改json的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。