{"record":{"id":"f97eca882962b360","repo":"cakephp/cakephp","slug":"invalid-data-provided-for-array-combine-to-work-both-name","errorCode":null,"errorMessage":"Invalid data provided for array_combine() to work: Both $name and $value require same count.","messagePattern":"Invalid data provided for array_combine\\(\\) to work: Both \\$name and \\$value require same count\\.","errorType":"exception","errorClass":"CakeException","httpStatus":null,"severity":"error","filePath":"src/View/View.php","lineNumber":889,"sourceCode":"        return $this->viewVars[$var] ?? $default;\n    }\n\n    /**\n     * Saves a variable or an associative array of variables for use inside a template.\n     *\n     * @param array|string $name A string or an array of data.\n     * @param mixed $value Value in case $name is a string (which then works as the key).\n     *   Unused if $name is an associative array, otherwise serves as the values to $name's keys.\n     * @return $this\n     * @throws \\Cake\\Core\\Exception\\CakeException If the array combine operation failed.\n     */\n    public function set(array|string $name, mixed $value = null)\n    {\n        if (is_array($name)) {\n            if (is_array($value)) {\n                $data = array_combine($name, $value);\n                if ($data === false) {\n                    throw new CakeException(\n                        'Invalid data provided for array_combine() to work: Both $name and $value require same count.',\n                    );\n                }\n            } else {\n                $data = $name;\n            }\n        } else {\n            $data = [$name => $value];\n        }\n        $this->viewVars = $data + $this->viewVars;\n\n        return $this;\n    }\n\n    /**\n     * Get the names of all the existing blocks.\n     *\n     * @return array<string> An array containing the blocks.","sourceCodeStart":871,"sourceCodeEnd":907,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/View/View.php#L871-L907","documentation":"View::set() supports passing two parallel arrays: a list of variable names and a list of values, combined via array_combine(). array_combine() returns false when the arrays have different lengths, and the method throws rather than silently dropping variables. When $value is not an array, $name itself is treated as the name=>value map and this check does not apply.","triggerScenarios":"Calling $view->set(['a','b'], [1]) or any call where $name and $value are both arrays but count($name) !== count($value).","commonSituations":"Programmatically building name/value lists that drift out of sync; bulk-assigning view vars from a loop where one variable was filtered out of one array but not the other.","solutions":["Ensure both arrays have the same number of elements","Prefer passing a single associative array: $view->set(['a' => 1, 'b' => 2])","Count/assert both arrays before calling set()"],"exampleFix":"// before\n$view->set(['title', 'items'], [$items]);\n// after\n$view->set(['title' => 'Hello', 'items' => $items]);","handlingStrategy":"validation","validationCode":"if (is_array($name) && is_array($value) && count($name) !== count($value)) {\n    throw new \\InvalidArgumentException('$name and $value must have equal length');\n}\n$view->set($name, $value);","typeGuard":null,"tryCatchPattern":"try {\n    $view->set($names, $values);\n} catch (\\Cake\\Core\\Exception\\CakeException $e) {\n    $view->set(array_combine_safe($names, $values));\n}","preventionTips":["Prefer a single associative array argument to set()","Assert equal array lengths before parallel-array calls","Build name/value maps together in one loop, not two"],"tags":["view","array","argument-count"],"backgroundTag":"invalid-argument-value","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}