简体   繁体   English

访问stdclass对象的索引

[英]Access an index of an stdclass object

I have an stdClass object that looks like this: 我有一个看起来像这样的stdClass对象:

[0] => stdClass Object
        (
            [id] => 123
            [name] => John Doe
            [date_created] => 13552412
        ) 

Is there a way to access an stdclass object by its index number and not through its name? 有没有一种方法可以通过其索引号而不是名称来访问stdclass对象?

You can convert it to an array with numeric keys: 您可以使用数字键将其转换为数组:

$array = array_values(get_object_vars($obj));
echo $array[1]; // John Doe

You mean you want to access its elements with a numerical index? 您是说要使用数字索引访问其元素?

$array = array_values((array) $object);

echo $array[0]; // id
echo $array[1]; // name
echo $array[2]; // date_created

I'm not really sure what you mean. 我不太确定你的意思。 If you could elaborate, I can edit my answer. 如果您能详细说明,我可以编辑我的答案。

or you can access it like this 或者您可以像这样访问它

foreach ($objects as $obj) {
    echo $obj->id;
    echo $obj->name;
    echo $obj->date_created;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM