Error "Cannot use object of type stdClass as array"

Error "Cannot use object of type stdClass as array"

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The stdClass Trap in PHP JSON Decoding

You've decoded a JSON string using json_decode() and immediately verified the contents with print_r(), yet accessing specific keys triggers a fatal error: Cannot use object of type stdClass as array. This is one of the most common friction points when working with JSON in PHP, stemming from a subtle default behavior of the decoding function that developers often overlook.

The issue isn't with your data or the decoding logic itself; it's with how you are calling json_decode(). By default, this function returns an instance of the stdClass class rather than a native associative array. When you inspect the object with print_r(), PHP displays the keys and values in a human-readable format that mimics an array, which is why the data appears correct at first glance. However, PHP objects and arrays are distinct data types. You cannot use array syntax (like $result['context']) to access properties on an stdClass object.

To fix this specific error, you must explicitly request an associative array as the return type. The solution is straightforward: pass true as the second argument to your json_decode() call.

$jsonString = '{"context": "some data", "status": 200}';

// This returns an object (stdClass)
$result = json_decode($jsonString); 
// Accessing $result['context'] here will trigger the fatal error.

// This returns an associative array
$result = json_decode($jsonString, true);
// Now $result['context'] works perfectly because $result is now an array.

By enabling the second parameter, you force PHP to unflatten the JSON structure into a standard array type. This aligns with how most developers expect JSON data to behave in procedural scripts or when interfacing with languages that treat JSON natively as dictionaries.

Handling Nested Structures

While casting the root level to an array solves the immediate error, it is worth noting that this behavior applies recursively only if you want full consistency. If your JSON contains nested objects, json_decode($data, true) will convert those nested objects into arrays as well. This is generally preferable for dynamic data structures where you don't know the exact schema beforehand, as it allows you to iterate over keys without needing to cast every single variable individually.

However, if you are dealing with a deeply nested structure and prefer to keep certain parts as objects (perhaps for performance reasons in very large datasets or when using specific reflection tools), you might need to selectively decode. In most general-purpose applications, though, the true flag is the standard approach.

The Object Approach: A Valid Alternative

If you are working within an Object-Oriented context and prefer type safety, sticking with stdClass is a valid architectural choice. Objects in PHP offer distinct advantages: they can have methods attached to them, and their properties are strongly typed if declared as such (though standard stdClass properties are dynamic). If you choose this route, you must access data using property syntax rather than array keys.

$result = json_decode($jsonString); // Returns stdClass by default

// Valid for objects
echo $result->context; 

// Invalid for objects (triggers the error seen in your scenario)
echo $result['context']; 

Laravel and Modern Ecosystems

In frameworks like Laravel, this distinction is often abstracted away or handled differently depending on how you load the data. When using json_decode() directly in raw PHP scripts or Blade templates outside of a framework's model binding, the default object behavior is strictly enforced by the core language.

If you are building an API response or consuming third-party JSON feeds in a Laravel application, ensure your service layer handles this transformation early. Often, it is cleaner to decode with true at the point of ingestion so that the rest of your logic treats the data as arrays. This prevents intermittent errors when iterating over collections or accessing deeply nested keys dynamically.

Remember that while stdClass objects are perfectly valid for serialization back into JSON (via json_encode()), mixing access methods based on the underlying type can lead to confusing runtime exceptions. Always be explicit about whether you want an object or an array at the moment of decoding, and stick to that choice throughout your data processing pipeline.

Our network