{"record":{"id":"f771752fbf257149","repo":"twigphp/Twig","slug":"the-find-filter-expects-a-sequence-or-a-mapping-got-s","errorCode":null,"errorMessage":"The \"find\" filter expects a sequence or a mapping, got \"%s\".","messagePattern":"The \"find\" filter expects a sequence or a mapping, got \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":2079,"sourceCode":"        self::checkArrow($isSandboxed, $arrow, 'filter', 'filter');\n\n        if (\\is_array($array)) {\n            return array_filter($array, $arrow, \\ARRAY_FILTER_USE_BOTH);\n        }\n\n        // the IteratorIterator wrapping is needed as some internal PHP classes are \\Traversable but do not implement \\Iterator\n        return new \\CallbackFilterIterator(new \\IteratorIterator($array), $arrow);\n    }\n\n    /**\n     * @param \\Closure $arrow\n     *\n     * @internal\n     */\n    public static function find(Environment $env, bool $isSandboxed, $array, $arrow)\n    {\n        if (!is_iterable($array)) {\n            throw new RuntimeError(\\sprintf('The \"find\" filter expects a sequence or a mapping, got \"%s\".', get_debug_type($array)));\n        }\n\n        self::checkArrow($isSandboxed, $arrow, 'find', 'filter');\n\n        foreach ($array as $k => $v) {\n            if ($arrow($v, $k)) {\n                return $v;\n            }\n        }\n\n        return null;\n    }\n\n    /**\n     * @param \\Closure $arrow\n     *\n     * @internal\n     */","sourceCodeStart":2061,"sourceCodeEnd":2097,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L2061-L2097","documentation":"The Twig `find` filter (CoreExtension::find) requires its first argument to be iterable — an array, Traversable, or object implementing iterable. Twig throws this RuntimeError when the value piped into `find` is a scalar (string, int, bool, null, etc.) instead of a sequence or mapping. It is thrown at runtime in PHP, not at template compile time, because the argument type cannot always be statically known.","triggerScenarios":"Calling `{{ [1,2,3]|find(v => v > 1) }}` where the value before the filter is actually a scalar — e.g. `{{ some_var|find(v => v) }}` where some_var is null, a string, or an int. The guard is `if (!is_iterable($array))` at CoreExtension.php:2079.","commonSituations":"A variable that is usually an array is null on this code path (e.g. a missing relation or unset context key); a function returns false/null on failure instead of an array; a doctrine collection was not initialized; passing a scalar from config into a filter chain.","solutions":["Guard the value in the template before filtering: use the `default` filter or an `is iterable` test, e.g. `{{ (items ?? [])|find(...) }}`.","Fix the data source so the variable actually contains an array/Traversable (check the controller/context assignment).","If the value may legitimately be a scalar, branch with `{% if items is iterable %}...{% else %}...{% endif %}`.","Catch Twig\\Error\\RuntimeError around the render call to surface the offending variable name via the template trace."],"exampleFix":"// before (Twig)\n{{ user.roles|find(role => role == 'ADMIN') }}\n\n// after\n{% if user.roles is iterable %}\n  {{ user.roles|find(role => role == 'ADMIN') }}\n{% endif %}","handlingStrategy":"validation","validationCode":"// PHP, before rendering\ndef ensure_iterable_for_find($value): void {\n    if (!is_iterable($value)) {\n        throw new InvalidArgumentException('find() input must be iterable, got ' . get_debug_type($value));\n    }\n}","typeGuard":"function isIterable($v): bool { return is_array($v) || $v instanceof \\Traversable; }","tryCatchPattern":"try {\n    $html = $twig->render('tpl.html.twig', $context);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (str_contains($e->getMessage(), '\"find\" filter expects a sequence or a mapping')) {\n        // log offending variable, render fallback\n    } else { throw $e; }\n}","preventionTips":["Always default nullable collections with `?? []` in Twig before piping into filters","In PHP controllers, normalize context arrays with `$value ?? []`","Make repository/service methods return `[]` instead of null/false on empty results","Use `{% if x is iterable %}` guards around filter chains on data of uncertain shape"],"tags":["twig","filter","type-mismatch","runtime-error"],"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"}