0

How can I detect 2nd and 3rd iteration

Here's what I did but It doesn't give the right answer

$sample_array = array('boom 1','boom 2','boom 3','boom 4','boom 5','boom 6','boom 7');

                            $separator2 = 0;
                            $separator3 = 0;
                            foreach($sample_array as $sample_array_value){
                                if(++$separator3 % 3 == 0)
                                {
                                    echo $sample_array_value."<br /><br /> Separator 3 <br /><br />";
                                }
                                else if(++$separator2 % 2 == 0)
                                {
                                    echo $sample_array_value."<br /><br /> Separator 2 <br /><br />";
                                }
                                else
                                {
                                    echo $sample_array_value."<br />";
                                }
                            }

the output of that code is:

boom 1
boom 2

Separator 2 

boom 3

Separator 3 

boom 4
boom 5

Separator 2 

boom 6

Separator 3 

boom 7

Which is wrong, I need the output to be:

boom 1
boom 2

Separator 2 

boom 3

Separator 3 

boom 4

Separator 2 

boom 5
boom 6

Separator 2 
Separator 3 

boom 7

2 Answers 2

3

You don't need extra variables, since your array is 0-based indexed, you can just use the key. Also you have to change your logic a bit, so that you get your expected output, e.g.

foreach($sample_array as $key => $sample_array_value){

    echo $sample_array_value . "<br />";

    if(($key + 1) % 2 == 0 && ($key + 1) % 3 == 0)
        echo "<br>Separator 2 <br />Separator 3<br /><br />";
    elseif(($key + 1) % 2 == 0)
        echo "<br>Separator 2 <br /><br />";
    elseif(($key + 1) % 3 == 0)
        echo "<br>Separator 3 <br /><br />";

}

output:

boom 1
boom 2

Separator 2 

boom 3

Separator 3 

boom 4

Separator 2 

boom 5
boom 6

Separator 2 
Separator 3

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

4 Comments

how can I use that without $key?, Cause in using mysql just like this foreach(mysql_fetch_array($query) as $get){}
@JoelEnanodJr Then initialize a variable before the foreach loop, e.g. $count = 0; and increment it at the end of the foreach loop, e.g. $count++;
Thank you sir @Rizier123, Even I don't Understand your code It solved my problem.. Im noob in loop hehe thanks much... by the way what's the meaning of e.g.?
@JoelEnanodJr You're welcome. (e.g. means something like: "for example")
0

You're suppose to check for 2nd and 3rd outside the if that implements the boom. since you also want the separator to be done after the boom, then bring it down.

$sample_array = array('boom 1','boom 2','boom 3','boom 4','boom 5','boom 6','boom 7');

                        $separator2 = 0;
                        $separator3 = 0;
                        $count = 1;
                        foreach($sample_array as $sample_array_value){
                            echo $sample_array_value."<br />";
                            if($count % 3 == 0)
                            {
                                echo "Separator 3 <br />";
                            }
                            if($count % 2 == 0)
                            {
                                echo "Separator 2 <br /><br />";
                            }
                            $count++;

                        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.