# Fatal error: Uncaught Error: Cannot use object of type stdClass as array

**Code:** `Cannot use object as array` · **Category:** PHP · **Seen on:** PHP

Code used array syntax (`$x['key']`) on an object.

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/php/cannot-use-object-as-array)
