【发布时间】:2014-09-18 15:41:43
【问题描述】:
我需要使用 PHP 将新对象附加到 JSON 数组。
JSON:
{
"maxSize":"3000",
"thumbSize":"800",
"loginHistory":[
{
"time": "1411053987",
"location":"example-city"
},
{
"time": "1411053988",
"location":"example-city-2"
}
]}
到目前为止的 PHP:
$accountData = json_decode(file_get_contents("data.json"));
$newLoginHistory['time'] = "1411053989";
$newLoginHistory['location'] = "example-city-3";
array_push($accountData['loginHistory'],$newLoginHistory);
file_put_contents("data.json", json_encode($accountData));
在保存 JSON 文件时,我不断将“null”作为“loginHistory”对象的输出。
【问题讨论】: