1

Hello I am having some trouble when it comes to iterating through my array chunks.

I am trying to put together an image gallery in which sets of 3 images follow a pattern then the next set of three follow the opposite of the pattern. I am pretty sure I am close, but I can not quite seem to figure out how exactly to select either the 1st, 2nd, 3rd, etc. chunk of my array with a for loop.

This is a print_r of my 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 then have the two patterns which print out three images in a certain pattern, the problem is I am unsure how to loops through my chunks and select the evens to follow pattern1 and the odds to follow pattern2. Below is the code I am currently using to attempt this:

foreach($chunks as $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 am still planning on refactoring the foreach loop, I just want to see it working before I write the two functions. The final output I am trying to achieve looks similar to this:

|       || small |
| Large || small |
| small ||       |
| small || Large |

Thanks in advance

3
  • maybe you were referring to chunk[index] as opposed to using chunks[index] Commented Jun 24, 2014 at 13:51
  • This was 100% a problem with the images not showing up >.< I am going to put an edit in real fast; however, now the images only show up thru the else statement. The first array chunk should follow the if and the second the else, so on and so forth. Commented Jun 24, 2014 at 13:54
  • check out Jim's answer, should shed some light regarding your modulo usage with if else, maybe instead of comparing the array with modulo, compare it to the array's keys $key % 2 == 0 something Commented Jun 24, 2014 at 13:55

3 Answers 3

1

Assuming your $chunks array is iterative (not associated) - meaning your array keys are numeric, you can grab the index like such:

foreach($chunks as $index => $chunk) {
    if($index % 2 == 0) {
        // stuff for even indexes
    } else {
        // stuff for odd indexes
}

You can also play around with $index % 3 (for every three elements) and so on.

Sign up to request clarification or add additional context in comments.

3 Comments

Just due to clarity of your answer I will mark it as correct, though for whoever else may read this be sure to read kevinabelita's comments. I don't think you can make comments marked as the correct answer.
@wr3tched no worries pal, just glad it shed some light
kevinabelita's comment below wasn't clear as to where you were to add the $key => $chunk snippet - plus, I think I missed his comment while I was writing my answer, otherwise I would have referenced it.
1

You're iterating over $chunks inside your loop the current item is $chunk. However you're using $chunks.

So things like:

echo "<img src='".$chunks[0]."' alt='gallery1'/>";

Should probably be:

echo "<img src='".$chunk[0]."' alt='gallery1'/>";

You do something similar in the if statement:

$chunks % 2 == 0

Also note that since $chunk is an array you can't do $chunk % 2, Try count($chunk) % 2 instead.

3 Comments

Thanks for the note about iterating over $chunks rather than $chunk. Also the count($chunk) will only output the number in the $chunk so in this case 3.
@wr3tched Ah, sorry. I misunderstood. I thought it was based on the entries in a chunk. I'll defer to the answer by jsickles.
Not a problem at all, I appreciate the quick help!!
0

To alternate between your two design, you can't do $chunks % 2 == 0. Instead, you can initialize a $i variable to 0, and then iterate it each loop. It's on this $i you can aply the modulo.

Example :

$i =0;
foreach($chunks as $chunk) 
{
   print_r($chunk);
   if($i % 2 == 0){
      // Design 1
   }
   else
   {
      // design 2
   }
   $i++;
}

Be careful, you made a mistake, you wanted $chunk[0] to show the images, not $chunks[0]

3 Comments

I made sure to change this in the edit already. Thanks, give me a moment to see if the i is what I was missing, though I believe it is.
actually you really dont need another variable, and another increment, just add $key => $chunk then if($key % 2 == 0) should be enough
By "=>" I take it you mean $key == $chunk, or am I mistaken? edit Nevermind in the foreach loop is what you meant. Sorry for simple question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.