在PHP中解码和更新JSON数组
我需要更新文件jsonData_0_123.JSON中的JSON数组。我可以从文件中获取数据并对其进行解码,但我无法更新文件中的数据(在本例中,数组将应答)在PHP中解码和更新JSON数组,php,arrays,json,decode,Php,Arrays,Json,Decode,我需要更新文件jsonData_0_123.JSON中的JSON数组。我可以从文件中获取数据并对其进行解码,但我无法更新文件中的数据(在本例中,数组将应答) 我尝试了您的代码,它正在json文件中使用数组答案进行更新。我发现(jsonData_0_123.json)将数据写入其中的文件权限错误有问题。否则它工作正常。请解释发生了什么情况。错误消息?你能给出var\u export($contentsdeconded)的输出吗? $contents = file_get_contents('jso
我尝试了您的代码,它正在json文件中使用数组答案进行更新。我发现(jsonData_0_123.json)将数据写入其中的文件权限错误有问题。否则它工作正常。请解释发生了什么情况。错误消息?你能给出
var\u export($contentsdeconded)的输出吗代码>?
$contents = file_get_contents('json_files/jsonData_0_123.json');
//Decode the JSON data into a PHP array.
$contentsDecoded = json_decode($contents, true);
//Update the decoded array
$contentsDecoded[1]["answers"] = "hello";
//Encode the array back into a JSON string.
$json = json_encode($contentsDecoded);
//Save the file.
file_put_contents('json_files/jsonData_0_123.json', $json);