Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中解码和更新JSON数组_Php_Arrays_Json_Decode - Fatal编程技术网

在PHP中解码和更新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

我需要更新文件jsonData_0_123.JSON中的JSON数组。我可以从文件中获取数据并对其进行解码,但我无法更新文件中的数据(在本例中,数组将应答)


我尝试了您的代码,它正在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);