{"record":{"id":"7854d096e69729ac","repo":"twigphp/Twig","slug":"the-column-filter-expects-a-sequence-or-a-mapping-got-s","errorCode":null,"errorMessage":"The \"column\" filter expects a sequence or a mapping, got \"%s\".","messagePattern":"The \"column\" filter expects a sequence or a mapping, got \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":2025,"sourceCode":"     *  {% set items = [{ 'fruit' : 'apple'}, {'fruit' : 'orange' }] %}\n     *\n     *  {% set fruits = items|column('fruit') %}\n     *\n     *  {# fruits now contains ['apple', 'orange'] #}\n     * </pre>\n     *\n     * @param array|\\Traversable $array An array\n     * @param int|string         $name  The column name\n     * @param int|string|null    $index The column to use as the index/keys for the returned array\n     *\n     * @return array The array of values\n     *\n     * @internal\n     */\n    public static function column(Environment $env, bool $isSandboxed, $array, $name, $index = null): array\n    {\n        if (!is_iterable($array)) {\n            throw new RuntimeError(\\sprintf('The \"column\" filter expects a sequence or a mapping, got \"%s\".', get_debug_type($array)));\n        }\n\n        if ($array instanceof \\Traversable) {\n            $array = iterator_to_array($array);\n        }\n\n        if ($isSandboxed) {\n            // The sandbox might be enabled via a SourcePolicyInterface, in which case the SandboxExtension\n            // would not consider the sandbox active without the current Source: $isSandboxed is already\n            // computed against the call-site source, so check the policy directly to honor that decision.\n            $policy = $env->getExtension(SandboxExtension::class)->getChecker()->getSecurityPolicy();\n            foreach ($array as $item) {\n                if (\\is_object($item)) {\n                    $policy->checkPropertyAllowed($item, (string) $name);\n                    if (null !== $index) {\n                        $policy->checkPropertyAllowed($item, (string) $index);\n                    }\n                }","sourceCodeStart":2007,"sourceCodeEnd":2043,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L2007-L2043","documentation":"The Twig 'column' filter (CoreExtension::column) extracts a column from an array of arrays/objects. It throws this RuntimeError when the input is not iterable (not array and not Traversable), e.g. null, a scalar, or a single entity.","triggerScenarios":"Using {{ value|column('name') }} where value is null, false, an int/string, or an object that is not Traversable — typically when the data source returned no list or a single record.","commonSituations":"Doctrine repository returning null on find() instead of an array; a query returning one row instead of a collection; unserialized/missing request data passed to the filter; API responses yielding null on error.","solutions":["Coerce the input to an array before the filter (e.g. (value ?? [])|column('name'))","Fix the upstream data provider to always return an array/Collection","Use the 'iterable' test to branch in the template: {% if items is iterable %}...{% endif %}"],"exampleFix":"// before\n{{ users|column('email') }}\n\n// after\n{{ (users ?? [])|column('email') }}","handlingStrategy":"validation","validationCode":"if (!is_iterable($users)) { $users = []; }\n// or in template: {% if users is iterable %}{{ users|column('email') }}{% endif %}","typeGuard":"function isListLike($v): bool { return is_array($v) || $v instanceof \\Traversable; }","tryCatchPattern":"try { $out = $twig->render($tpl, $ctx); } catch (\\Twig\\Error\\RuntimeError $e) { if (str_contains($e->getMessage(), '\"column\" filter')) { /* normalize input to array */ } }","preventionTips":["Default nullable query results to [] before passing to templates","Use the iterable test in templates before collection filters","Ensure repositories return arrays/Collections, never null"],"tags":["twig","filter","column","iterable"],"backgroundTag":"incompatible-source-type","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"}