Reputation: 24061
I have a loop:
foreach ($arr as $_k => $_v) {
if($_k % 6 == 0){
//do something
}
}
I need "something" to happen every sixth loop, but I need an offset of 2. So it would happen in loop 2, 8, 14 etc.
How can this be achieved?
Furthermore, I also need the "something" to happen on offsets of 4 too. So 4, 10, 16 etc.
Is this possible in the same operator or would I need an or statement?
Upvotes: 0
Views: 1307
Reputation: 2220
Simple enough
$_k % 6 == 2
Or more generally
$_k % 6 == offset
For your case if you need to do something different at offset 2 and 4, you will need to do two if statements.
if ($_k % 6 == 2)
{
...
} else if ($_k % 6 == 4)
{
...
}
Upvotes: 0
Reputation: 798676
Just change the comparison.
if ($_k % 6 == 2) {
...
}
if ($_k % 6 == 4) {
...
}
Upvotes: 3