Cannot use object as array 🐘 PHP

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

  1. json_decode($json, true) for arrays
  2. Or access properties with ->
  3. Cast with (array) $obj for simple objects

Detailed fix by platform

PHP

  1. $data = json_decode($body, true, 512, JSON_THROW_ON_ERROR); echo $data['name'];

How to diagnose

  1. Type — var_dump() — object or array?
  2. 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.