PHP Error: Cannot use object of type stdClass as array (array and object issues)

PHP Error: Cannot use object of type stdClass as array (array and object issues)

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Error: Cannot use object of type stdClass as array (array and object issues)

If you are encountering the runtime error Cannot use object of type stdClass as array, the issue is almost certainly in how your data source is structured versus how your code expects to access it. This specific error occurs when you attempt to use square bracket notation—like $variable['key']—on a variable that is an instance of stdClass, which is PHP's default object structure for objects created with new stdClass() or returned by some JSON decoders, rather than a true array.

In the example provided where the error occurs, the code iterates over a collection using foreach. Inside that loop, the script attempts to access properties like $blog['id'] and $blog['title']. If your data source (perhaps a database query or an API response) returns objects instead of associative arrays, this syntax fails immediately. While PHP allows accessing object properties via dot notation ($object->property), it does not natively support bracket notation for standard objects unless they implement the ArrayAccess interface.

To resolve this, you must first inspect the actual type of your data. You can do this by adding a temporary var_dump($blog) or print_r($blog) inside your loop. If the output shows stdClass, you have two primary options for fixing the code: switch to dot notation or convert the object to an array.

Switching to dot notation is often the simplest fix if you are dealing with objects that map directly to class properties. However, this approach assumes a strict naming convention between your property names (e.g., id, title) and your database columns or JSON keys. If your data source uses snake_case keys like user_name but your PHP object has a property named $userName, dot notation will fail to find the value unless you explicitly map it.

The more robust solution, especially when working with dynamic data from external APIs or JSON responses, is to convert the stdClass instance into an associative array using the (array) cast. This allows you to retain your existing bracket notation code without modifying every single property access. Here is how you can refactor the problematic loop:

foreach ($blogs as $blog) {
    // Convert stdClass object to an associative array if necessary
    $blogArray = (array) $blog; 

    $id      = $blogArray['id'];
    $title   = $blogArray['title'];
    $content = $blogArray['content'];

    echo "<h1>" . $title . "</h1>";
    echo "<p>" . $content . "</p>";
}

By casting $blog to (array), PHP iterates over the object's properties and treats them as keys in an associative array. This is a common pattern when handling data from libraries or frameworks that default to returning objects for JSON decoding, such as json_decode($jsonString, false). Note that if you used json_decode($jsonString, true) instead, the result would be an array by default, preventing this error entirely.

There are trade-offs to consider here. Casting everything to an array adds a slight computational overhead for every iteration, though it is usually negligible in modern PHP versions. Conversely, relying on dot notation can make your code brittle if the underlying data structure changes or if property names do not match exactly. For large-scale applications, understanding the distinction between these types helps you write more defensive code. You might want to add a check at the start of your loop:

foreach ($blogs as $blog) {
    if (is_array($blog)) {
        // Handle array directly
    } elseif (is_object($blog)) {
        // Cast object or handle as object properties
        $id = $blog->id; 
    }
}

If you are working within the Laravel ecosystem, this specific error is less common because Laravel's Eloquent ORM and query builder typically return collections of objects that map cleanly to models, or they provide methods to easily convert results. However, if you are fetching raw data from an API using HttpClient or similar tools, you will frequently encounter stdClass instances. Understanding how to handle these types ensures your application remains stable regardless of where the data originates.

Our network