Maaz Zindani

Loading the index…

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

PHP says Array to string conversion and prints the word Array

ErrorWarning: Array to string conversion

Short 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.

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

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1echo $items; or 'Items: ' . $items prints ArrayAn array is echoed or joined into a stringFix
    2. 2implode(', ', $rows) returns text with Array inside itimplode gets an array that contains arraysFix
    3. 3The warning appears at ->save() or ->update(), not in a viewAn Eloquent attribute holds an array but has no castFix

What does the error look like?

  • Warning: Array to string conversion
  • Notice: 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

Did this fix it?

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

Did this fix it?

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.php

2Fix

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()->options

Confirmed by laravel.com

Did this fix it?

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 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.