Mackski Posted February 3, 2014 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++; } }
Soma Posted February 3, 2014 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
Mackski Posted February 3, 2014 Author Posted February 3, 2014 Thanks Soma, I guess adding a chunk() would save a few keystrokes.
Soma Posted February 3, 2014 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.
ryan Posted February 7, 2014 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
Mackski Posted February 11, 2014 Author 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.
kongondo Posted February 11, 2014 Posted February 11, 2014 Slice is right here: http://processwire.com/api/arrays/ and here http://cheatsheet.processwire.com/ 2
interrobang Posted February 11, 2014 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
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