如何修改php中的json數(shù)據(jù)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
php修改json數(shù)據(jù)的方法:首先從文件中讀取數(shù)據(jù)到PHP變量;然后把JSON字符串轉(zhuǎn)成PHP數(shù)組;最后通過(guò)“file_put_contents("text.json",$json_strings);”方法修改json數(shù)據(jù)即可。
php 讀寫(xiě)json文件、追加、修改json
// 追加寫(xiě)入用戶(hù)名下文件 $code="001";//動(dòng)態(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);//寫(xiě)入 //修改 $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);//寫(xiě)入
關(guān)于如何修改php中的json數(shù)據(jù)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。