Home > Article > Backend Development > How to Handle the \"Notice: Trying to get property of non-object\" Error in PHP?
"Notice: Trying to Get Property of Non-Object" Error in PHP
When attempting to retrieve data from the API using PHP, you may encounter the error "Notice: Trying to get property of non-object." This issue arises when the variable holding the API response is an array of objects, but the code attempts to access a property of an object as if it were a regular array element.
Solution:
To resolve this issue, we need to access the array element, which is an object, before accessing its attributes:
<code class="php">$pjs = json_decode($js); echo $pjs[0]->player_name;</code>
In this example, $pjs[0] retrieves the first element of the array, which is an object. Then, we can access the object's properties, such as player_name, using the -> operator.
The above is the detailed content of How to Handle the \"Notice: Trying to get property of non-object\" Error in PHP?. For more information, please follow other related articles on the PHP Chinese website!