Fatal error: Uncaught Error: Cannot use object of type stdClass as array
Code used array syntax ($x['key']) on an object.
Seen on:
PHP
Meaning
json_decode returns objects by default (stdClass), so $data['name'] fails. Pass true to get arrays, or use object syntax $data->name.
Common causes
- json_decode without the associative flag
- API client returning objects
- Mixing arrays and objects in the same codebase
⚡ Quick fix
json_decode($json, true)for arrays- Or access properties with
-> - Cast with
(array) $objfor simple objects
Detailed fix by platform
PHP
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR); echo $data['name'];
How to diagnose
- Type — var_dump() — object or array?
- Source — Where was it created?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026