0

我有这种情况:

object(stdClass)#203 (1) {
  ["1"]=>
  object(stdClass)#212 (7) {
    ["user_id"]=>
    int(1)
    ["type"]=>
    string(6) "Device"
    ["name_first"]=>
    string(0) ""
    ["name_last"]=>
    string(0) ""
    ["name_display"]=>
    string(0) ""
    ["gender"]=>
    string(11) "Unspecified"
    ["birthday"]=>
    string(0) ""
  }
}

我想访问“user_id”或[“1”],但这是stdclass类型,所以我不能像数组一样对待它。(注:来自json_decode

4

2 回答 2

2

您必须将其转换为数组:

$obj = (array) $obj;
$obj[1]["user_id"]
于 2012-07-23T19:14:33.027 回答
1

json_decode()当设置为的第二个参数TRUE将自动将您的结果转换为关联数组:

$objs = json_decode($json, true);
echo $objs[1]["user_id"];
于 2012-07-23T19:14:38.893 回答