]> git.andy128k.dev Git - missing-tools.git/commitdiff
more generic code in Collections::columns
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 22 Sep 2013 19:32:38 +0000 (22:32 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 22 Sep 2013 19:32:38 +0000 (22:32 +0300)
src/collectiontools.php

index b2bfc0457415ee041c91ab65dc38a8ee882b5710..eb2a42dcaf56f38fa349e3164725a5b42b7085f4 100644 (file)
@@ -13,13 +13,23 @@ final class Collections
         $count = count($collection);
         $r = $count % $columnsCount;
         $length = ($count - $r) / $columnsCount;
-        $columns = array();
-        $offset = 0;
+
+        $columnLengths = array();
         for ($i = 0; $i < $columnsCount; ++$i) {
-            $l = $length + ($i < $r ? 1 : 0);
-            $columns[] = array_slice($collection, $offset, $l);
-            $offset += $l;
+            $columnLengths[] = $length + ($i < $r ? 1 : 0);
+        }
+
+        $column = array();
+        $columnIndex = 0;
+        foreach ($collection as $item) {
+            if (count($column) >= $columnLengths[$columnIndex]) {
+                $columns[] = $column;
+                $column = array();
+                $columnIndex++;
+            }
+            $column[] = $item;
         }
+        $columns[] = $column;
         return $columns;
     }