我有一个看起来像这样的对象:
stdClass Object
(
[data] => stdClass Object
(
[users] => stdClass Object
(
[831C7119-FAEF-49AF-B95B-50FB492B4ACB] => stdClass Object
(
[lastName] => somename
[username] => email@email.com
[userExtId] => 831C7119-FAEF-49AF-B95B-50FB492B4ACB
[fullName] => somename somename
[firstName] => somename
[displayName] => bla bla bla
[extId] => 831C7119-FAEF-49AF-B95B-50FB492B4ACB
[password] => 12345678
)
是否可以访问say $ object-> data-> users-> lastname?
编辑:
function objectToArray($d) {
if (is_object($d))
$d = get_object_vars($d);
return is_array($d) ? array_map(__METHOD__, $d) : $d;
}
答案 0 :(得分:6)
请看一下使用variable variables语法:
$object->data->users->{'831C7119-FAEF-49AF-B95B-50FB492B4ACB'}->lastName
如果您想将整个对象转换为数组(source):
function objectToArray($d) {
if (is_object($d))
$d = get_object_vars($d);
return is_array($d) ? array_map(__METHOD__, $d) : $d;
}
$arr = objectToArray($object);
$arr['data']['users']['831C7119-FAEF-49AF-B95B-50FB492B4ACB']['lastName']
答案 1 :(得分:2)
尝试:
$object->data->users->{'831C7119-FAEF-49AF-B95B-50FB492B4ACB'}->lastName;
答案 2 :(得分:0)
在具有二维的多维关联数组 $user
中,您通常访问命名键,如
echo $user[413]['name'];
如果你有一个与上面数组结构相同的对象$user
,你可以通过这种方式访问属性
echo $user[413]->name;