@@ -171,37 +171,14 @@ function changeColumnMaxWidth($table, $headers, $columns, $maxWidth)
171171 */
172172function addTableSeparator ($ rows )
173173{
174- $ separatedRows ;
175-
176- foreach ($ rows as $ key => $ value ) {
177- // If the array is NOT a numerical indexed list,
178- // ie: it's an associative array...
179- if (!array_is_list ($ rows )) {
180- // Set the array element as it was originally.
181- $ separatedRows [$ key ] = $ value ;
182-
183- // If element is not the last, then add the table separator.
184- if ($ key !== array_key_last ($ rows )) {
185- $ separatedRows [] = new TableSeparator ();
186- }
187- }
188- // Otherwise, it's an indexed list...
189- else {
190- // If element is the first, then add it as it originally was.
191- if ($ key === array_key_first ($ rows )) {
192- $ separatedRows [$ key ] = $ value ;
193- }
194- // Otherwise, increment the index (so that that it doesn't
195- // interfere with the separator).
196- else {
197- $ separatedRows [$ key + 1 ] = $ value ;
198- }
199- // If element is not the last, then add the table separator.
200- if ($ key !== array_key_last ($ rows )) {
201- $ separatedRows [$ key + 1 ] = new TableSeparator ();
202- }
203- }
204- }
174+ /**
175+ * Create a new laravel collection and add the table separator
176+ * inbetween all the rows.
177+ * Code from https://laracasts.com/discuss/channels/site-improvements/php-array-insert-between-each-item
178+ */
179+ $ separatedRows = collect ($ rows )->flatMap (function ($ item ) {
180+ return [$ item , new TableSeparator ()];
181+ })->slice (0 , -1 )->toArray ();
205182
206183 return $ separatedRows ;
207184}
0 commit comments