2
A
回答
7
使用模數來確定偏移量。
$i = 0;
foreach ($array as $a) {
$i++;
if ($i % 5 == 0) {
// your code for every 5th item
}
// your inside loop code
}
6
除非您在每次迭代中單獨做某事,否則不要。
for循環使用,並通過5每次遞增計數器:
$collectionLength = count($collection);
for($i = 0; $i < $collectionLength; i+=5)
{
// Do something
}
否則,你可以使用模運算符來確定,如果你是在第五次迭代的一個:
if(($i + 1) % 5 == 0) // assuming i starts at 0
{
// Do something special this time
}
1
for($i = 0; $i < $items; $i++){
//for every 5th item, assuming i starts at 0 (skip)
if($i % 5 == 0 && $i != 0){
//execute your code
}
}
相關問題
- 1. (JavaScript)的同步foreach循環裏面
- 2. Foreach裏面的功能
- 3. 查詢裏面的forEach
- 4. Makefile foreach裏面如果
- 5. 當裏面的foreach刪除,我們需要後退一步
- 6. 異步函數不是foreach循環裏面等待
- 7. 刪除與LINQ裏面的foreach那裏
- 8. 返回裏面短信內部foreach
- 9. cshtml查看裏面的其他foreach
- 10. 訪問查詢裏面的foreach codeigniter
- 11. 使用如果綁定裏面foreach(KnockoutJS)
- 12. 等待裏面的foreach關鍵詞,
- 13. 如何在裏面添加`if`或foreach?
- 14. 陣列在foreach裏面的位置
- 15. JSTL繼續,破解裏面的foreach
- 16. Cakephp裏面找到一個foreach
- 17. 的foreach對數組的數組裏面
- 18. Laravel把裏面的foreach郵件
- 19. C#爲繼續裏面的foreach
- 20. LINQ表達不與在裏面的foreach
- 21. 的foreach裏面的foreach奇怪的錯誤
- 22. 使Foreach異步
- 23. 試圖串聯運行的異步和同步,裏面的foreach,使空的結果
- 24. 是forEach()異步嗎?
- 25. 訪問裏面外面的foreach變量在PowerShell中
- 26. 爲什麼在裏面等待同步?
- 27. jquery getJSON裏面的getJSON同步
- 28. 插入行裏面PHP
- 29. mysql表裏面加入
- 30. 嵌入.pdf裏面.swf
與模運算符%...($ I%5)== 1 ... – 2012-04-11 21:41:35
使用模http://php.net/manual/en/language.operators.arithmetic。 PHP的 – Drewdin 2012-04-11 21:44:23
可能重複[PHP的foreach迭代兩次而不是一次](http://stackoverflow.com/questions/10072015/php-foreach-iterate-twice-instead-of-once) – GordonM 2012-04-11 22:01:28