[英]Every 3rd Iteration of PHP Loop
When I say 'Every 3rd Iteration' I mean 'Every 3rd Iteration... starting from number 4' 当我说“每个第三迭代”时,我的意思是“从数字4开始的每个第三迭代...”
If it were just every 3rd, I would target the appropriate iterations like so: 如果仅仅是每3次,我将针对适当的迭代,如下所示:
<?php if ($count % 3 == 0) : ?>
Bear in mind I've set $count
to 1
beforehand 请记住,我已经将
$count
预先设置为1
But the iterations need to start with the 4th, then 7th, 10th etc. 但是迭代需要从第4个,第7个,第10个等开始。
Does anyone know how I can acheive this? 有谁知道我能做到这一点吗? Can I do it with a simple
if
statement like the one above? 我可以使用上面的简单
if
语句吗?
Unfortunately I don't think a for
loop will be possible as this is a WordPress loop I'm dealing with 不幸的是,我认为
for
循环不可能,因为这是我正在处理的WordPress循环
Your thoughts about using the modulus operator are sound, you just need to expand the scope of your logic surrounding it: 您对使用模数运算符的想法很合理,您只需要扩展围绕它的逻辑范围即可:
You want every third iteration after the initial 4 have passed. 您希望在最初的4次通过后每三次迭代一次。 Begin your logic after the first 4:
在前4个之后开始您的逻辑:
if ($count > 4)
Then, you want each 3 after that. 然后,您想要每3个。 Your counter includes the initial 4 iterations you didn't want to include, so remove that from your counter and check if the current iteration is a multiple of 3:
您的计数器包含了您不希望包含的前4个迭代,因此从计数器中删除它,并检查当前迭代是否为3的倍数:
if (($count - 4) % 3 === 0)
This should give you what you're looking for. 这应该给您您想要的东西。
if ($count > 4)
{
if (($count - 4) % 3 === 0)
{
...
}
}
You can also put this into one line: 您还可以将其放入一行:
if ($count > 4 && ($count - 4) % 3 === 0)
{
...
}
foreach($array as $key=>$value){
if($key % 3 != 0){
continue;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.