stdClass as Array: The PHP Trap You'll Avoid

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

You Write Code, and PHP Shouts: "Don't Touch Me Like an Array!"

\n\n

Imagine: you've received data from an API. Everything looks great, the JSON is parsed, and you're sure you have an array. You access an element by key — and instead of a result, you see Fatal error: Cannot use object of type stdClass as array. Sound familiar? This error is one of the most common traps for beginner (and not-so-beginner) PHP developers. It's been discussed thousands of times on Stack Overflow. Today, we won't just explain why it happens; we'll compare 5 approaches to bypass or prevent it. You'll learn which method suits your task and forever forget the panic when you see stdClass.

\n\n

This article is for those who write in PHP, work with JSON, REST APIs, or databases. We speak in Russian, but with live code examples. Let's go!

\n\n

Why Does the Error Occur? The Root of Evil — stdClass

\n\n

PHP has a built-in class stdClass (standard class). It is used as an "empty" object when, for example, you call json_decode() without the second parameter. By default, json_decode returns an object, not an array. And if you then try to access its property using square brackets — PHP complains.

\n\n
<?php\n$json = '{"name": "Anna", "age": 25}';\n$data = json_decode($json); // returns stdClass object\necho $data['name']; // Fatal error: Cannot use object of type stdClass as array\n?>\n
\n\n

An object and an array in PHP are different types. Object properties are accessed via ->, array elements via []. They cannot be mixed. But what if you don't control the data source? Or if legacy code is all tied to arrays? Let's figure it out.

\n\n

Approach 1: Type Casting (array) — Fast, but with Surprises

\n\n

The most obvious way is to explicitly cast the object to an array using (array). This will work, but not always as you expect.

\n\n
<?php\n$json = '{"name": "Anna", "age": 25}';\n$data = (array) json_decode($json);\necho $data['name']; // Anna — works!\n?>\n
\n\n

All good? Not quite. If the object contains nested objects, they are not converted recursively. stdClass objects will remain inside, and trying to access them as an array will return the error.

\n\n
<?php\n$json = '{"user": {"name": "Anna", "age": 25}}';\n$data = (array) json_decode($json);\necho $data['user']['name']; // Error again! user is an object\n?>\n
\n\n

When to use: for flat JSON structures without nesting. Fast, but shallow.

\n\n

Approach 2: json_decode with the true Flag — The Gold Standard

\n\n

The most popular and reliable way is to pass true as the second argument to json_decode. Then the function returns an associative array, not an object. And nested structures will also be arrays.

\n\n
<?php\n$json = '{"user": {"name": "Anna", "age": 25}}';\n$data = json_decode($json, true); // true — return array\necho $data['user']['name']; // Anna — no errors\n?>\n
\n\n

This approach is recommended in PHP documentation and the community. It solves the problem at its root without requiring additional conversions.

\n\n

When to use: always when working with JSON and expecting an array. The only downside is if you forget to add true.

\n\n

Approach 3: Recursive Conversion — For Complex Structures

\n\n

Sometimes data comes not only from JSON but also from other sources: databases, SOAP, XML parsers. And you get a ready-made object that needs to be turned into an array at all costs. A recursive function will help here.

\n\n
<?php\nfunction objectToArray($data) {\n    if (is_object($data)) {\n        $data = get_object_vars($data);\n    }\n    if (is_array($data)) {\n                

Blogs

Book Recommendations