Jump to content

Split image array into groups of 15


a-ok
 Share

Recommended Posts

Hi folks,

I have an image field, that has 50+ images (the total count varies per project) and I want to split them into groups of 15 (so if the total count is 50 the groups would be 15, 15, 15, 5 and if the count is 12 then it just shows twelve). I'd then be keen to loop through each 'group'.

Any thoughts how I would do this using the PW API? Is it possible with only the API or would I have to use some non-API PHP?

I was thinking I could use splice with a count but unsure if I need to create a new array per group each time?

Link to comment
Share on other sites

5 minutes ago, abdus said:

$imgs = $page->images->getArray();
$chunks = array_chunk($imgs, 15);

foreach($chunks as $chunk) {
	foreach($chunk as $img) { /* ... */ }
}

http://php.net/manual/en/function.array-chunk.php

Unfortunately, there doesnt seem to be a chunk method available for WireArray class
https://processwire.com/api/ref/wire-array/

Whoa array_chunk is great. That's good to know that PW API doesn't have a chunk method... always try to use the PW methods. Much appreciated and apologies if that felt like a simple PHP question!

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...