ModestGrey ModestGrey - 1 year ago 71
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 Source

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'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download