Jump to content

Recommended Posts

Posted

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++;
            }
        }

Posted
$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.

  • Like 1
Posted

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.

Posted

How about this: 

for($size = 4, $n = 0; $n < count($images); $n += $size) {
  foreach($images->slice($n, $size) as $img) {
    echo $img->url;
  }
} 
  • Like 1

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
  • Recently Browsing   0 members

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