我有带有数组的 json 文件,我想使用 php 将数组元素添加到该数组。请建议我继续。
这是我现有的 json 文件
数据.json
{
"12":{"userId":"1","username":"Ss","password":"33"},
"32":{"userId":"1","username":"Ss","password":"33"}
}
我的 php 文件
下面是我的代码部分,我需要将其附加到现有的 json 文件中。
$data[$thisId] = array(
'userId'=> $userId,
'username'=> $username,
'password'=> $password,
);
$json = json_encode($data);
如何将其添加到我的 json 文件中?请指教
: 我想将来自 php 文件的数据写入现有的 json 文件
最佳答案
您必须解码文件才能附加数据:
$data = json_decode(file_get_contents('data.json'), true);
$data[$thisId] = array(
'userId'=> $userId,
'username'=> $username,
'password'=> $password,
);
$json = json_encode($data);
file_put_contents('data.json', $json);
关于javascript - 使用 php 将 json 文件附加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42815521/