Ffile2fix
Sign in Get started

How to fix "Array to string conversion" in PHP

Warning: Array to string conversion in /home/user/public_html/checkout.php on line 64

An array was used where PHP expected a string, for example in echo or with the . operator. PHP replaces it with the literal word 'Array' and raises a warning (a notice before PHP 8).

Also appears as: Notice: Array to string conversion (PHP 7) · PHP Warning: Array to string conversion in wp-includes/formatting.php · Array to string conversion (page shows the literal word 'Array')

Common causes

  • Echoing a multi-value form field such as checkboxes named tags[] or a multi-select
  • Concatenating the result of a function that returns an array (explode(), get_post_meta() without true)
  • Building SQL or URLs by inserting an array into a string
  • Using an array inside double-quoted string interpolation like "$items"
  • Passing an array to a function that expects a string, where PHP coerces it

How to fix it

  1. Find the array on the line. var_dump() each value used in echo or concatenation on that line to see which one is an array.
  2. Pick the element you meant. If you wanted one value, index it ($tags[0]) or fix the function call, e.g. get_post_meta($id, 'key', true) returns a single value.
  3. Join lists with implode(). For display, use implode(', ', $tags) and escape the result with htmlspecialchars().
  4. Serialize for storage or APIs. Use json_encode($array) when storing in a text column, a cookie or sending to JavaScript.
  5. Use placeholders for SQL IN lists. Never inject an array into SQL; build one ? per item with str_repeat and pass the array to execute().
  6. Debug with print_r. For debugging output use print_r($arr, true) or var_export() inside <pre> tags.

PHP

$tags = $_POST['tags'] ?? [];
echo htmlspecialchars(implode(', ', $tags));

$in = implode(',', array_fill(0, count($ids), '?'));
$stmt = $pdo->prepare("SELECT * FROM products WHERE id IN ($in)");
$stmt->execute($ids);

How to stop it happening again

  • Know which form fields can submit arrays
  • Pass the $single = true argument to get_post_meta() and similar functions
  • Use typed parameters (string $name) so arrays fail early

Frequently asked questions

Why does my page show the word 'Array'?

That is PHP's string form of an array. Something on the page echoed an array instead of one of its values.

Why does get_post_meta() return an array?

Without the third argument it returns all values for that key as an array. Pass true to get a single value.

Is this a fatal error?

No, it is a warning and the script continues, but the output is wrong. If the array reaches a function with a string type hint, it becomes a TypeError instead.