cakephp/cakephp · error · LogicException

Child arrays do not have even length

Error message

Child arrays do not have even length

What it means

transpose() swaps rows and columns of a collection of arrays, like a matrix transpose. It requires every child array to have the same length; the first element defines the expected length and any row with a different count triggers this LogicException.

Solutions

  1. Normalize all rows to the same length before transposing (pad with null or truncate)
  2. Filter out rows that don't match the expected length: ->filter(fn ($r) => count($r) === $expected)
  3. Fix the data source so every row has the same number of fields
  4. Wrap in try-catch (LogicException) and handle ragged input separately

Example fix

// before
$t = $collection->transpose(); // [[1,2],[3,4,5]]
// after
$max = max($collection->map(fn ($r) => count($r))->toList());
$normalized = $collection->map(fn ($r) => $r + array_fill(count($r), $max - count($r), null));
$t = $normalized->transpose();
Defensive patterns

Strategy: validation

Validate before calling

$rows = $collection->toList();
if (count($rows) > 1 && count(array_unique(array_map('count', $rows))) > 1) {
    throw new \UnexpectedValueException('Ragged rows: cannot transpose');
}

Type guard

function isRectangular(array $rows): bool
{
    if (!$rows) { return true; }
    $len = count(reset($rows));
    return count($rows) === 1 || count(array_unique(array_map('count', $rows))) === 1;
}

Try / catch

try {
    $t = $collection->transpose();
} catch (\LogicException $e) {
    $t = normalizeRows($collection)->transpose();
}

Prevention

When it happens

Trigger: Calling ->transpose() where elements are arrays of unequal length, e.g. [[1,2,3],[4,5]]; first element is empty while others are not (or vice versa).

Common situations: Transposing CSV-like rows where some rows have missing trailing fields; ragged API response rows; forgetting to normalize/pad records before transposing.

Related errors


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

Appendix: source

Thrown at src/Collection/CollectionTrait.php:1194

        return $this->newCollection($result); // @phpstan-ignore return.type
    }

    /**
     * {@inheritDoc}
     *
     * @return \Cake\Collection\CollectionInterface<int, array<mixed>>
     * @throws \LogicException
     */
    public function transpose(): CollectionInterface
    {
        $arrayValue = $this->toList();
        /** @phpstan-ignore argument.type (transpose requires array values) */
        $length = count(current($arrayValue));
        $result = [];
        foreach ($arrayValue as $row) {
            /** @phpstan-ignore argument.type (transpose requires array values) */
            if (count($row) !== $length) {
                throw new LogicException('Child arrays do not have even length');
            }
        }

        for ($column = 0; $column < $length; $column++) {
            $result[] = array_column($arrayValue, $column);
        }

        return $this->newCollection($result); // @phpstan-ignore return.type
    }

    /**
     * @inheritDoc
     */
    public function count(): int
    {
        $traversable = $this->optimizeUnwrap();

        if (is_array($traversable)) {

View on GitHub (pinned to 1128eba9b0)