Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP fatal error: Cannot use object of type stdClass as array

ErrorCannot use object of type stdClass as array

Short answer

You wrote $row['name'] on a value that is an object. PHP objects need the arrow, $row->name. The usual sources are json_decode, which returns objects unless you pass true as the second argument, and query results that come back as one stdClass object per row.

  • Applies to PHP 8.3, Laravel 12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the type and shape of the value on the line that fails. It tells you if you hold an object or an array.

    var_dump($row);
  2. 2Match what you see, then jump to its fix

    1. 1var_dump shows object(stdClass) right after a json_decode calljson_decode returned objectsFix
    2. 2var_dump of a row shows object(stdClass) after DB::select or a similar callA database query returned one object per rowFix
    3. 3The data was an array in one function and an object in anotherAn array was cast to an object earlierFix

What does the error look like?

  • Cannot use object of type stdClass as array

Cause 1 of 3

json_decode returned objects

1Check

With no second argument, json_decode turns every JSON object into a stdClass object. Only an associative flag of true gives arrays.

$data = json_decode($json);
var_dump($data);

2Fix

Pass true as the second argument if you want array syntax, or keep the objects and read them with the arrow.

$data = json_decode($json, true);
echo $data['name'];

// or keep objects
$data = json_decode($json);
echo $data->name;

3Verify

The line that failed now prints the value.

php -r '$d = json_decode("{\"a\":1}", true); echo $d["a"];'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A database query returned one object per row

1Check

Laravel's DB::select returns an array of stdClass objects, one for each record, and the docs read the values with the arrow. Code written for array rows then fails.

var_dump(DB::select('select * from users limit 1'));

2Fix

Read the columns as properties.

foreach ($users as $user) {
    echo $user->name;
}

3Verify

The loop prints each name with no error.

var_dump($users[0]->name);

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

An array was cast to an object earlier

1Check

The (object) cast turns array keys into properties, so array access stops working after it.

grep -rn "(object)" app/ src/ 2>/dev/null

2Fix

Use one shape for the value across the code, and read it with the matching syntax.

$obj = (object) ['name' => 'Sam'];
echo $obj->name;

3Verify

The value is read the same way everywhere it is used.

var_dump($obj);

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Decide once whether a layer passes arrays or objects, and keep to it.
  • Add return types or doc comments so the shape of a value is visible.
  • Always pass the second argument to json_decode on purpose.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.