{"record":{"id":"31be4b57b98ee500","repo":"cakephp/cakephp","slug":"child-arrays-do-not-have-even-length","errorCode":null,"errorMessage":"Child arrays do not have even length","messagePattern":"Child arrays do not have even length","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Collection/CollectionTrait.php","lineNumber":1194,"sourceCode":"        return $this->newCollection($result); // @phpstan-ignore return.type\n    }\n\n    /**\n     * {@inheritDoc}\n     *\n     * @return \\Cake\\Collection\\CollectionInterface<int, array<mixed>>\n     * @throws \\LogicException\n     */\n    public function transpose(): CollectionInterface\n    {\n        $arrayValue = $this->toList();\n        /** @phpstan-ignore argument.type (transpose requires array values) */\n        $length = count(current($arrayValue));\n        $result = [];\n        foreach ($arrayValue as $row) {\n            /** @phpstan-ignore argument.type (transpose requires array values) */\n            if (count($row) !== $length) {\n                throw new LogicException('Child arrays do not have even length');\n            }\n        }\n\n        for ($column = 0; $column < $length; $column++) {\n            $result[] = array_column($arrayValue, $column);\n        }\n\n        return $this->newCollection($result); // @phpstan-ignore return.type\n    }\n\n    /**\n     * @inheritDoc\n     */\n    public function count(): int\n    {\n        $traversable = $this->optimizeUnwrap();\n\n        if (is_array($traversable)) {","sourceCodeStart":1176,"sourceCodeEnd":1212,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/Collection/CollectionTrait.php#L1176-L1212","documentation":"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.","triggerScenarios":"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).","commonSituations":"Transposing CSV-like rows where some rows have missing trailing fields; ragged API response rows; forgetting to normalize/pad records before transposing.","solutions":["Normalize all rows to the same length before transposing (pad with null or truncate)","Filter out rows that don't match the expected length: ->filter(fn ($r) => count($r) === $expected)","Fix the data source so every row has the same number of fields","Wrap in try-catch (LogicException) and handle ragged input separately"],"exampleFix":"// before\n$t = $collection->transpose(); // [[1,2],[3,4,5]]\n// after\n$max = max($collection->map(fn ($r) => count($r))->toList());\n$normalized = $collection->map(fn ($r) => $r + array_fill(count($r), $max - count($r), null));\n$t = $normalized->transpose();","handlingStrategy":"validation","validationCode":"$rows = $collection->toList();\nif (count($rows) > 1 && count(array_unique(array_map('count', $rows))) > 1) {\n    throw new \\UnexpectedValueException('Ragged rows: cannot transpose');\n}","typeGuard":"function isRectangular(array $rows): bool\n{\n    if (!$rows) { return true; }\n    $len = count(reset($rows));\n    return count($rows) === 1 || count(array_unique(array_map('count', $rows))) === 1;\n}","tryCatchPattern":"try {\n    $t = $collection->transpose();\n} catch (\\LogicException $e) {\n    $t = normalizeRows($collection)->transpose();\n}","preventionTips":["Pad or trim rows to a uniform length before transposing","Validate source data (e.g. CSV parsing) rejects rows with missing fields","Use array_pad/arrayslice normalization as a standard pre-step","Test transpose with edge cases: empty first row, single row, single column"],"tags":["php","cakephp","collection","transpose","shape-mismatch"],"backgroundTag":"shape-mismatch","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}