PHP fatal error: Cannot use object of type stdClass as array
Cannot use object of type stdClass as arrayShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
var_dump shows object(stdClass) right after a json_decode calljson_decode returned objectsFix - 2
var_dump of a row shows object(stdClass) after DB::select or a similar callA database query returned one object per rowFix - 3
The data was an array in one function and an object in anotherAn array was cast to an object earlierFix
- 1
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
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
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/null2Fix
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
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 says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack 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 fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack 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 fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix