Array_chunk in foreach don't work

Hi,
I want to split an foreach of images. For this I use array_chunk().
But I don’t get any image output if I us it.

<?php                                                                                                                                                                                                                                                                               
$thumbs = $page->images();
$i = 1;
/* foreach($thumbs as $image): */
foreach (array_chunk($thumbs, 4, true) as $image):
$container_class = ($i == 1) ? 'item active' : 'item';
?>

Can anybody explain me why this not work and how I can get it to work?

Cheers

It does not work because $thumbs is not an array. You can use offset() and limit().

1 Like

If you want to use array_chunk, you can also convert the images collection to an array using:

$thumbs = $page->images()->toArray();

However I think that a native chunk method for collections is a good idea. I have added it as a feature request on GitHub.

1 Like

There’s now a $collection->chunk() method in Kirby 2.3.2.

2 Likes