ModestGrey ModestGrey - 6 months ago 12
PHP Question

PHP - Cannot access array in stdclass object

Currently I have a stdclass object. How can I access the properties of the object such as the name of the author? I have tried to echo out

$authors = $book->authors_summary[0]->name;
but it returns 'Trying to get property of non-object in ...' error.

[authors_summary] => Array
(
[0] => Array
(
[id] => 123456789
[name] => Ben Smith
)

[1] => Array
(
[nconst] => 987654321
[name] => Tommy Lee
)

)

Answer

Your authors_summary[0] is an array, so you cannot use -> object pointer, you must call it as array:

$authors = $book->authors_summary[0]['name'];
Comments