I'm always encountering problems that require multiple nested foreach loops but I always devised ways of avoiding these nested loops. I avoided them because I needed to break out of the outer loops and wasn't able to.
What I needed was *continue N;* and *break N;*
Then I realized that you can break out of nested loops very easily. Like so:
foreach ($values as $value) {
echo "Outer Foreach Loop\n";
for ($i = 1; ; $i++) {
echo " Middle For Loop\n";
while (1) {
echo " Inner While Loop\n";
continue 3;
}
echo "This never gets output.\n";
}
echo "Neither does this.\n";
}
while ($i++ < 5) {
echo "Outer\n";
while (1) {
echo " Middle While Loop\n";
while (1) {
echo " Inner While Loop\n";
break 3; // To break out of the 3 While Loops
}
echo "This never gets output.\n";
}
echo "Neither does this.\n";
}
By using "continue N;" or "break N;" you can break out of the nested loops