a-ok Posted August 22, 2017 Share Posted August 22, 2017 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 More sharing options...
abdus Posted August 22, 2017 Share Posted August 22, 2017 $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 classhttps://processwire.com/api/ref/wire-array/ 3 Link to comment Share on other sites More sharing options...
a-ok Posted August 22, 2017 Author Share Posted August 22, 2017 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 classhttps://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 More sharing options...
Robin S Posted September 14, 2017 Share Posted September 14, 2017 On 23/08/2017 at 1:51 AM, abdus said: Unfortunately, there doesnt seem to be a chunk method available for WireArray class FYI, a while ago a wrote a little tutorial demonstrating how an array_chunk method can be added to WireArray: 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now