cakephp/cakephp · error · CakeException

Invalid data provided for array_combine() to work: Both…

Error message

Invalid data provided for array_combine() to work: Both $name and $value require same count.

What it means

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.

Solutions

  1. Ensure both arrays have the same number of elements
  2. Prefer passing a single associative array: $view->set(['a' => 1, 'b' => 2])
  3. Count/assert both arrays before calling set()

Example fix

// before
$view->set(['title', 'items'], [$items]);
// after
$view->set(['title' => 'Hello', 'items' => $items]);
Defensive patterns

Strategy: validation

Validate before calling

if (is_array($name) && is_array($value) && count($name) !== count($value)) {
    throw new \InvalidArgumentException('$name and $value must have equal length');
}
$view->set($name, $value);

Try / catch

try {
    $view->set($names, $values);
} catch (\Cake\Core\Exception\CakeException $e) {
    $view->set(array_combine_safe($names, $values));
}

Prevention

When it happens

Trigger: Calling $view->set(['a','b'], [1]) or any call where $name and $value are both arrays but count($name) !== count($value).

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/f97eca882962b360. Report an issue: GitHub.

Appendix: source

Thrown at src/View/View.php:889

        return $this->viewVars[$var] ?? $default;
    }

    /**
     * Saves a variable or an associative array of variables for use inside a template.
     *
     * @param array|string $name A string or an array of data.
     * @param mixed $value Value in case $name is a string (which then works as the key).
     *   Unused if $name is an associative array, otherwise serves as the values to $name's keys.
     * @return $this
     * @throws \Cake\Core\Exception\CakeException If the array combine operation failed.
     */
    public function set(array|string $name, mixed $value = null)
    {
        if (is_array($name)) {
            if (is_array($value)) {
                $data = array_combine($name, $value);
                if ($data === false) {
                    throw new CakeException(
                        'Invalid data provided for array_combine() to work: Both $name and $value require same count.',
                    );
                }
            } else {
                $data = $name;
            }
        } else {
            $data = [$name => $value];
        }
        $this->viewVars = $data + $this->viewVars;

        return $this;
    }

    /**
     * Get the names of all the existing blocks.
     *
     * @return array<string> An array containing the blocks.

View on GitHub (pinned to 1128eba9b0)