我有一個這樣的循環中循環:保持計數遍及多個for循環
//stuff here to determine what $my_var will be
for($i=0;$i<count($my_var);$i++) {
//stuff here to determine what $anothervar will be
for ($y = 1; $y <= $anothervar; $y++) {
//help needed in here
echo $y; //makes it so count starts over each time it goes around
}
}
my_var將會循環一定量的時候,並不總是相同的金額。
內循環也是一個隨機數。
的輸出看起來是這樣的:
1
1,2
2
3
4
5
6
1,2,3
所以在第一主迴路內循環發生兩次。在第六主循環中,內循環發生3次。
我想要做的是,而不是每次從1開始的內循環,我希望它繼續增加。所以我想輸出是這樣的:
1
1,2
2
3
4
5
6
3,4,5
比方說,第3主迴路中有一些內部循環,我們將使它4個內部循環,那麼輸出應該是這樣的:
1
1,2
2
3
3,4,5,6
4
5
6
7,8,9
我該如何做一個循環內循環連續計數?
編輯
這裏是結束了工作:
//stuff here to determine what $my_var will be
$y = 1;
for($i=0;$i<count($my_var);$i++) {
//stuff here to determine what $anothervar will be
for (; $y <= $anothervar; $y++) {
//help needed in here
echo $y; //this now continues to count up instead of starting over each main loop
}
$y = 1;
}
有人回答,我打算標記爲正確答案,因爲它足夠接近。我所做的只是添加第二個y = 1,但那個人刪除了他們的答案。 – leoarce
對不起,我的錯誤,我做的編輯也不正確。回到繪圖板。 – leoarce
發佈循環的完整代碼...需要了解什麼影響第二個循環。 – IROEGBU