Mackski Posted February 3, 2014 Share Posted February 3, 2014 I tend to chunk images quite often for <table> elements in emails, pdfs and other layouts. array_chunk($page->images) does not work, it would be great if there was a method to chunk your images. eg: foreach($images->chunk(4) as $chunk) { foreach($chunk as $img) { echo $img->url; } } I might be over thinking it, but I currently have to manually chunk like: $x = 0; $c = 0; foreach($stage->images as $i) { $chunks[$c][] = $i; if($x > 2) { $c++; $x = 0; } else { $x++; } } Link to comment Share on other sites More sharing options...
Soma Posted February 3, 2014 Share Posted February 3, 2014 $arr = $page->images->getArray(); $arr2 = array_chunk($arr,2); foreach($arr2 as $key => $chunk){ $content .= "chunk$key<br>"; foreach($chunk as $c){ $content .= "$c->url<br>"; } } Though not sure if getArray() really is optimal, but I guess so. But like the idea of chunk() for WireArrays maybe. 1 Link to comment Share on other sites More sharing options...
Mackski Posted February 3, 2014 Author Share Posted February 3, 2014 Thanks Soma, I guess adding a chunk() would save a few keystrokes. Link to comment Share on other sites More sharing options...
Soma Posted February 3, 2014 Share Posted February 3, 2014 The only drawback I see is that WireArray is always single level and not nested, so chunk() would have to change that concept. I think if you post this in Wishlist forum, Ryan will have to tell. Link to comment Share on other sites More sharing options...
ryan Posted February 7, 2014 Share Posted February 7, 2014 How about this: for($size = 4, $n = 0; $n < count($images); $n += $size) { foreach($images->slice($n, $size) as $img) { echo $img->url; } } 1 Link to comment Share on other sites More sharing options...
Mackski Posted February 11, 2014 Author Share Posted February 11, 2014 Slice would work. I take it that is not part of the core. I had a quick look through the source. Link to comment Share on other sites More sharing options...
kongondo Posted February 11, 2014 Share Posted February 11, 2014 Slice is right here: http://processwire.com/api/arrays/ and here http://cheatsheet.processwire.com/ 2 Link to comment Share on other sites More sharing options...
interrobang Posted February 11, 2014 Share Posted February 11, 2014 slice is a method of WireArray and multifile fields are WireArrays too. see here for available methods: http://processwire.com/api/arrays/ or http://cheatsheet.processwire.com/ 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