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
- 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.
- 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.
- Join lists with implode(). For display, use implode(', ', $tags) and escape the result with htmlspecialchars().
- Serialize for storage or APIs. Use json_encode($array) when storing in a text column, a cookie or sending to JavaScript.
- 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().
- 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