{"record":{"id":"85ea5ce7bfedc093","repo":"twigphp/Twig","slug":"the-merge-filter-expects-a-sequence-or-a-mapping-got-s-for","errorCode":null,"errorMessage":"The \"merge\" filter expects a sequence or a mapping, got \"%s\" for argument %d.","messagePattern":"The \"merge\" filter expects a sequence or a mapping, got \"(.+?)\" for argument (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":749,"sourceCode":"     * Merges any number of arrays or Traversable objects.\n     *\n     *  {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}\n     *\n     *  {% set items = items|merge({ 'peugeot': 'car' }, { 'banana': 'fruit' }) %}\n     *\n     *  {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car', 'banana': 'fruit' } #}\n     *\n     * @param array|\\Traversable ...$arrays Any number of arrays or Traversable objects to merge\n     *\n     * @internal\n     */\n    public static function merge(...$arrays): array\n    {\n        $result = [];\n\n        foreach ($arrays as $argNumber => $array) {\n            if (!is_iterable($array)) {\n                throw new RuntimeError(\\sprintf('The \"merge\" filter expects a sequence or a mapping, got \"%s\" for argument %d.', get_debug_type($array), $argNumber + 1));\n            }\n\n            $result = array_merge($result, self::toArray($array));\n        }\n\n        return $result;\n    }\n\n    /**\n     * Slices a variable.\n     *\n     * @param mixed $item         A variable\n     * @param int   $start        Start of the slice\n     * @param int   $length       Size of the slice\n     * @param bool  $preserveKeys Whether to preserve key or not (when the input is an array)\n     *\n     * @return mixed The sliced variable\n     *","sourceCodeStart":731,"sourceCodeEnd":767,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L731-L767","documentation":"The Twig `merge` filter (CoreExtension::merge) accepts one or more arguments and requires every argument to be iterable (array or Traversable); each is converted via toArray and merged with array_merge. If argument N is not iterable, a RuntimeError is thrown reporting the debug type and 1-based argument number.","triggerScenarios":"{{ a|merge(b) }} where a or b is null, a scalar, boolean, or a non-Traversable object; merge with several arguments where any one is non-iterable (the message reports which argument number).","commonSituations":"Merging a null variable produced by an optional query result; merging an stdClass from json_decode without array output; merging a string assuming it will be treated as a list; refactors where a variable changed from array to collection-less scalar.","solutions":["Ensure both operands are arrays: {{ a|merge(b) }} with a and b as arrays or Traversables.","Check the argument number in the message, dump that variable, and fix its origin.","Guard with defaults: {{ (a|default([]))|merge(b|default([])) }}.","Cast a non-Traversable object to array first or use |to_array-like conversions; for scalars, wrap as [value] if merging into a list is intended."],"exampleFix":"// before (template)\n{% set all = items|merge(null) %}\n\n// after\n{% set all = items|merge(extra|default([])) %}","handlingStrategy":"type-guard","validationCode":"// Twig template\n{% if a is iterable and b is iterable %}{% set all = a|merge(b) %}{% endif %}","typeGuard":"function canMerge(...$args): bool { foreach ($args as $a) { if (!\\is_iterable($a)) { return false; } } return true; }","tryCatchPattern":"try {\n    $out = \\Twig\\Extension\\CoreExtension::merge($a, $b);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    $out = \\array_merge(\\is_iterable($a) ? \\iterator_to_array($a) : [], \\is_iterable($b) ? \\iterator_to_array($b) : []);\n}","preventionTips":["Default nullable operands: {{ (a|default([]))|merge(b|default([])) }}.","Ensure JSON decoding produces arrays (json_decode($json, true)).","Check the argument number in the error message to locate the offending operand quickly."],"tags":["twig","filter","type-error","merge"],"backgroundTag":"type-mismatch","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}