Browse Source

Fix Collection::transpose() with different number of columns and rows

Fixes #10137
Rodrigo moyle 9 years ago
parent
commit
5fec593769

+ 3 - 0
src/Collection/CollectionTrait.php

@@ -748,6 +748,9 @@ trait CollectionTrait
             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);
         }
 

+ 5 - 4
tests/TestCase/Collection/CollectionTest.php

@@ -1932,13 +1932,14 @@ class CollectionTest extends TestCase
             ['Product A', '200', '100', '50'],
             ['Product B', '300', '200', '100'],
             ['Product C', '400', '300', '200'],
+            ['Product D', '500', '400', '300'],
         ]);
         $transposed = $collection->transpose();
         $expected = [
-            ['Products', 'Product A', 'Product B', 'Product C'],
-            ['2012', '200', '300', '400'],
-            ['2013', '100', '200', '300'],
-            ['2014', '50', '100', '200'],
+            ['Products', 'Product A', 'Product B', 'Product C', 'Product D'],
+            ['2012', '200', '300', '400', '500'],
+            ['2013', '100', '200', '300', '400'],
+            ['2014', '50', '100', '200', '300'],
         ];
 
         $this->assertEquals($expected, $transposed->toList());