Accessing Data within Decoded JSON Object
When using json_decode() to decode JSON data, it's important to note that the returned value will be an object by default. However, sometimes we may encounter the error "Cannot use object of type stdClass as array," which occurs when we attempt to treat the decoded data as an array.
To resolve this issue, we can explicitly specify the second parameter of json_decode() as true, which will force it to return an array instead of an object. Here's an example:
$data = '{"context": "example"}'; $result = json_decode($data, true);
Now, the $result variable will contain an array, where we can access the "context" value using the $result['context'] notation.
The above is the detailed content of How to Access Data in a Decoded JSON Object?. For more information, please follow other related articles on the PHP Chinese website!