I have an stdClass object that looks like this:
[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?
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.