From f13fbb42d0d1a54bd709d374abfbcab63c18f7f7 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 22 Sep 2013 22:32:38 +0300 Subject: [PATCH] more generic code in Collections::columns --- src/collectiontools.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/collectiontools.php b/src/collectiontools.php index b2bfc04..eb2a42d 100644 --- a/src/collectiontools.php +++ b/src/collectiontools.php @@ -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; } -- 2.49.0