Accessing an array chunk in PHP -
hello having problem when comes iterating through array chunks.
i trying set image gallery in sets of 3 images follow pattern next set of 3 follow opposite of pattern. pretty sure close, can not quite seem figure out how select either 1st, 2nd, 3rd, etc. chunk of array loop.
this print_r of sample set.
array ( [0] => array ( [0] => images/uploads/cardinal.png [1] => images/uploads/fb.png [2] => images/uploads/logo.png ) [1] => array ( [0] => images/uploads/masc.png [1] => images/uploads/sportclubslogo.png [2] => images/uploads/venue.jpg ) ) i have 2 patterns print out 3 images in pattern, problem unsure how loops through chunks , select evens follow pattern1 , odds follow pattern2. below code using effort this:
foreach($chunks $chunk) { print_r($chunk); if($chunks % 2 == 0){ echo "<div class='row'> <div class='gal-img medium-8 large-8 columns'>"; echo "<img src='".$chunk[0]."' alt='gallery1'/>"; echo "</div> <div class='gal-img medium-4 large-4 columns'>"; echo "<img src='".$chunk[1]."' alt='gallery2'/>"; echo "<img src='".$chunk[2]."' alt='gallery3'/>"; echo "</div> </div>"; } else { echo "<div class='row'> <div class='gal-img medium-4 large-4 columns'>"; echo "<img src='".$chunk[0]."' alt='gallery4'/>"; echo "<img src='".$chunk[1]."' alt='gallery5'/>"; echo "</div> <div class='gal-img medium-8 large-8 columns'>"; echo "<img src='".$chunk[2]."' alt='gallery6'/>"; echo "</div> </div>"; } } i still planning on refactoring foreach loop, want see working before write 2 functions. final output trying accomplish looks similar this:
| || little | | big || little | | little || | | little || big | thanks in advance
assuming $chunks array iterative (not associated) - meaning array keys numeric, can grab index such:
foreach($chunks $index => $chunk) { if($index % 2 == 0) { // stuff indexes } else { // stuff odd indexes } you can play around $index % 3 (for every 3 elements) , on.
php arrays for-loop
No comments:
Post a Comment