Alternate Merging of Two Page Arrays

No need to convert to an array, since the class used for page collections already has methods for iterating and getting pages from a collection.
https://getkirby.com/docs/cheatsheet/pages/nth

For instance, as a controller:

<?php

return function($site, $pages, $page) {

   $collection = pages();
   $group1 = $site->find('section1')->children()->limit(10);
   $group2 = $site->find('section2')->children()->limit(10);
   $max    = max($group1->count(), $group2->count());

   for ($i = 0; $i < $max; $i++) {
      if ($p1 = $group1->nth($i)) {
         $collection->add($p1);
      }
      if ($p2 = $group2->nth($i)) {
         $collection->add($p2);
      }
   }

   return ['collection' => $collection];

};

2 Likes