小编Osc*_*gio的帖子

将数据附加到 json 负载时出现“致命错误:未捕获错误:无法使用 stdClass 类型的对象作为数组”

我需要使用 PHP 将一个新对象附加到 JSON 数组。

JSON:

{
   "maxSize":"3000",
   "thumbSize":"800",
   "loginHistory":[
   {
      "time": "1411053987",      
      "location":"example-city"
   },
   {
      "time": "1411053988",      
      "location":"example-city-2"
   }
]}
Run Code Online (Sandbox Code Playgroud)

到目前为止的 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));
Run Code Online (Sandbox Code Playgroud)

我不断得到:

致命错误:未捕获错误:无法将 stdClass 类型的对象用作数组

null作为保存 JSON 文件时“loginHistory”对象的输出。

php arrays json

5
推荐指数
1
解决办法
2万
查看次数

PHP:按键排序JSON数据

我想使用PHP按字母顺序按键排序单行JSON数据.所以最后:

{"one":"morning","two":"afternoon","three":"evening","four":"night"}
Run Code Online (Sandbox Code Playgroud)

变为:

{"four":"night","one":"morning","three":"evening","two":"afternoon"}
Run Code Online (Sandbox Code Playgroud)

我试过用ksort无济于事:

$icons = json_decode(file_get_contents("icons.json"));
ksort($icons);
foreach($icons as $icon => $code){...}
Run Code Online (Sandbox Code Playgroud)

php sorting json

2
推荐指数
1
解决办法
4041
查看次数

标签 统计

json ×2

php ×2

arrays ×1

sorting ×1