You are here

function twig_array_batch in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/twig/twig/lib/Twig/Extension/Core.php \twig_array_batch()

Batches item.

Parameters

array $items An array of items:

int $size The size of the batch:

mixed $fill A value used to fill missing items:

Return value

array

1 string reference to 'twig_array_batch'
Twig_Extension_Core::getFilters in vendor/twig/twig/lib/Twig/Extension/Core.php

File

vendor/twig/twig/lib/Twig/Extension/Core.php, line 1523

Code

function twig_array_batch($items, $size, $fill = null) {
  if ($items instanceof Traversable) {
    $items = iterator_to_array($items, false);
  }
  $size = ceil($size);
  $result = array_chunk($items, $size, true);
  if (null !== $fill && !empty($result)) {
    $last = count($result) - 1;
    if ($fillCount = $size - count($result[$last])) {
      $result[$last] = array_merge($result[$last], array_fill(0, $fillCount, $fill));
    }
  }
  return $result;
}