PHP says Array to string conversion and prints the word Array
Warning: Array to string conversionShort answer
PHP turns any array used as a string into the text Array and, since PHP 8.0, raises a warning. Find the line that echoes, concatenates or saves an array. Print it with print_r, pick one element, join a flat array with implode, or give the Eloquent attribute an array cast.
Fix it fast
1Find out which cause you have
The warning names a file and a line. Dump the value on that line to see that it really is an array, and what is inside it.
// put this on the line before the one in the warning var_dump($value);2Match what you see, then jump to its fix
- 1
echo $items; or 'Items: ' . $items prints ArrayAn array is echoed or joined into a stringFix - 2
implode(', ', $rows) returns text with Array inside itimplode gets an array that contains arraysFix - 3
The warning appears at ->save() or ->update(), not in a viewAn Eloquent attribute holds an array but has no castFix
- 1
What does the error look like?
Warning: Array to string conversionNotice: Array to string conversion (PHP 7)The page shows the word Array where text should be
Cause 1 of 3
An array is echoed or joined into a string
1Check
PHP's manual says arrays always convert to the string Array, so echo and print cannot show their contents. Since PHP 8.0 this is a warning, in PHP 7 it was a notice.
var_dump(is_array($items));2Fix
Read the element you want, or print the whole array in readable form. print_r with true returns the text instead of printing it.
echo $items['name'];
echo print_r($items, true);3Verify
Reload the page. The warning and the word Array are gone.
curl -s http://localhost:8000/page | grep -c 'Array'Confirmed by php.net
Cause 2 of 3
implode gets an array that contains arrays
1Check
implode joins the values of the array. If one value is itself an array, that value is converted to a string, so it becomes Array.
var_dump(array_filter($rows, 'is_array'));2Fix
Pull the field you need out of each inner array first, then join the flat list.
echo implode(', ', array_column($rows, 'name'));3Verify
The output is a plain comma list.
var_dump(implode(', ', array_column($rows, 'name')));Confirmed by php.net
Cause 3 of 3
An Eloquent attribute holds an array but has no cast
1Check
Laravel turns an array attribute into JSON for storage through the array cast. A model without that cast has nothing converting the array before it is saved.
grep -n "casts" -A6 app/Models/User.php2Fix
Add the array cast to the model. Laravel then serializes the array to JSON on set and back to an array on read.
protected function casts(): array
{
return [
'options' => 'array',
];
}3Verify
Assign an array and save.
php artisan tinker
>>> $u = App\Models\User::first(); $u->options = ['a' => 1]; $u->save();
>>> $u->fresh()->optionsConfirmed by laravel.com
How do I stop it coming back?
- Dump unknown values with var_dump or print_r while you debug, not echo.
- Declare array shapes in docblocks or types so static analysis flags an array used as a string.
- Cast array and JSON columns on the model.
- Treat warnings as errors in tests so this stops at the first run.
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