1

i have a strange behaviour with my script, the foreach loop only stops if i echo results, so it just continues to insert values... Only stops if i use echo to show some iterations... Below is important part of code:

$looparray=array
(
"1"=>array(
        "something1", 
        "something2",
        "something3",
                ),
"2"=>array(
                ),
"3"=>array(
                ),
"4"=>array(
                ),
"5"=>array(
                ),
"6"=>array(
                )
);
foreach ($looparray as $key => $value) {

    if(count($value)=='0') // skip empty arrays
    {
        continue;
    }

    foreach ($value as $singlevalue) {
        for ($i=0; $i<=5; $i++)
        {
            echo $i . '<br />';  //if i don't use it, neverending loop ?!
        }
    }
}

So if i'm not echo-ing then loop is not stopping at 5... Of course i have a lot of stop in the inner for loop, but those are not important... So why is it happening?

FirstOne
  • 6,033
  • 7
  • 26
  • 45
gergokee
  • 23
  • 1
  • 7
  • Your for loop is correct. I ran your code without the echo statement and it completed normally without an infinite loop. There must be something else going on in the rest of your code. – Charles R Dec 30 '15 at 17:39
  • _Of course i have a lot of stop in the inner for loop, but those are not important_ They might be. If this is not your complete code, it's going to be hard to help you... – FirstOne Dec 30 '15 at 18:30

2 Answers2

0

If you want to stop your looping just use unset($singlevalue); after the foreach loop.

Md Dalwar
  • 19
  • 2
-1

Use break to leave the loop.

Reading Material

break

Community
  • 1
  • 1
Script47
  • 14,230
  • 4
  • 45
  • 66
  • Hmm , im not a php noobie.... "break" is only good if i WANT TO leave the loop when a condition is met, however i don't want to leave the loop, i want it to go till it ends... But it does not end, that is my problem... – gergokee Dec 30 '15 at 22:54