Fix PHP array_map(): Argument #2 Must Be an Array

“array_map(): Argument #2 ($array) must be of type array, string given” means the value supplied as the input collection is not an array. Check the value at the boundary where it enters the application, then either reject it or deliberately normalize a permitted single value.

Last updated: October 30, 2026.

<?php
function normalizeTags(array|string|null $value): array
{
    if ($value === null || $value === '') {
        return [];
    }

    $tags = is_array($value) ? $value : [$value];

    return array_values(array_filter(array_map(
        static fn (mixed $tag): string => trim((string) $tag),
        $tags
    )));
}

$tags = normalizeTags($_POST['tags'] ?? null);

This function documents that one string, many strings, or no value are accepted. If the field must always be an array, replace normalization with a validation error so malformed input is not silently reinterpreted.

Find where the value changes type

The PHP manual signature for array_map() requires its second argument and every additional collection argument to be an array. A string commonly arrives when an HTML control changed from a multi-value field to a single-value field, JSON was decoded incorrectly, or a database column contains serialized text rather than decoded data.

Inspect the value with get_debug_type() and log only safe metadata. Avoid dumping an entire request because it may contain passwords, tokens, or personal data.

Do not hide an upstream data error

Using (array) $value everywhere can suppress the exception while preserving the wrong meaning. Define the accepted types in a function signature, request validator, or DTO. Decode JSON with error handling before mapping it, and verify that a decoded object is actually an array.

Once the collection is valid, match the callback parameters to the number of arrays supplied to array_map(). Continue with PHP array fundamentals, handling empty arrays safely, and understanding PHP throwable errors.

Related Web Cheat Sheet guides

Sergey Kornilov

Sergey Kornilov