1

我有一个看起来像这样的对象:

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
                    )

是否可以访问说 $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;
}
4

3 回答 3

6

看看为此使用变量变量语法:

$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']
于 2012-11-13T22:44:45.690 回答
2

尝试:

$object->data->users->{'831C7119-FAEF-49AF-B95B-50FB492B4ACB'}->lastName;
于 2012-11-13T22:45:43.570 回答
0

$user在具有二维的多维关联数组中,您通常访问命名键,例如

echo $user[413]['name'];

如果您有一个$user与上述数组具有相同结构的对象,您可以通过这种方式访问​​属性

echo $user[413]->name;
于 2021-02-07T17:07:47.860 回答