解码并更新 PHP 中的 JSON 数组
Decode and update a JSON array in PHP
我需要更新文件 jsonData_0_123.json 中的 JSON 数组。我可以从文件中获取数据并对其进行解码,但我无法更新文件中的数据(在这种情况下,数组答案)
$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);
我试过你的代码,它正在 json 文件中更新 answers.I 发现文件权限错误问题 (jsonData_0_123.json) 将数据写入 it.Otherwise 它正在工作很好。
我需要更新文件 jsonData_0_123.json 中的 JSON 数组。我可以从文件中获取数据并对其进行解码,但我无法更新文件中的数据(在这种情况下,数组答案)
$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);
我试过你的代码,它正在 json 文件中更新 answers.I 发现文件权限错误问题 (jsonData_0_123.json) 将数据写入 it.Otherwise 它正在工作很好。