Reputation:
I am trying to stop a foreach loop and a child foreach loop at a combined count of 8 but its not currently working and ends up echoing 15 times .heres the code I currently have
$i=0;
foreach($somethings as $something){
if (++$i == 8) break;
echo something;
foreach($subsomethings as $subsomething){
if (++$i == 8) break;
echo $subsomething
}
}
how do I close both foreach loops if $i == 8
Thanks
Upvotes: 0
Views: 956
Reputation: 168
You need to use ++$i >= 8
instead of ++$i == 8
. When the inner loop is broken the outer loop continues as $i is incremented to 9.
Upvotes: 2
Reputation: 854
Change the first if to be greater than or equal to:
if (++$i >= 8) break;
Upvotes: 0