要修改JSON中的特定鍵值對,可以按照以下步驟進行操作:
json_decode()
函數來實現:$json = '{"name":"John", "age":30, "city":"New York"}';
$arr = json_decode($json, true);
$arr['name'] = 'Tom';
$arr['age'] = 25;
json_encode()
函數來實現:$newJson = json_encode($arr);
完整的代碼示例:
$json = '{"name":"John", "age":30, "city":"New York"}';
$arr = json_decode($json, true);
$arr['name'] = 'Tom';
$arr['age'] = 25;
$newJson = json_encode($arr);
echo $newJson;
輸出結果:
{"name":"Tom","age":25,"city":"New York"}
通過這種方式,你可以輕松地修改JSON中的指定鍵值對。