title: PHP: variable scope in multiple foreach
body: <p>EDIT:<br>
  Problem solved by using $i+1 in function call.</p>
  
  <p>I have trouble using variables in multiple foreaches. The problem commeth when I try to call a function inside foreach. Whenever I do this the main loop's iterator value is suddenly zero (no matter which lap it's on) but when I comment out the function call the iterator value shows as it should again.</p>
  
  <p>Could someone point me to the right direction in accessing variables in following examples:</p>
  
  <p>This works as it shoulds</p>
  
  <pre><code>for($i=0; $i&lt;3; $i++)
  {
      echo $i; // 1, 2, 3
      foreach($something as $value)
      {
          echo $i; // main loop's iterator value
          foreach($value as $moreSomething)
          {
              echo $i; // main loop's iterator value 
          }
      }
  }
  </code></pre>
  
  <p>But this doesn't work, iterator shows up as a 0.</p>
  
  <pre><code>for($i=0; $i&lt;3; $i++)
  {
      echo $i; // 1, 2, 3
      foreach($something as $value)
      {
          echo $i; // 0
          foreach($value as $moreSomething)
          {
              echo $i; // 0
              $object-&gt;addStuff($i, $moreSomething); // i = 0, moreSomething is correct
          }
      }
  }
  </code></pre>
  
  <p>EDIT:
  I'm adding example code to reproduce the problem. Note that this is about the behaviour of $i's value, not the correct order of the names or something like that.
  I'm just concerned as why the $i's value suddenly changes. Might be my logic, but see for yourself</p>
  
  <p>(when using $i or $b in addName() function $i's output is 000000000012, when using $a then $i's output suddenly changes to 00000000001111111222)</p>
  
  <pre><code>class RockPaperScissors
  {
      private $nameArray;
  
    // constructor
    function RockPaperScissors () 
    {
      $this-&gt;nameArray = array();
    }
  
    function addName($level, $name)
    {
      $this-&gt;nameArray[$level][] = $name;
    }
  
    function getNames($level)
    {
      $array = array();
      foreach ($this-&gt;nameArray as $key =&gt; $value)
      {
          if ($key == $level)
          {
              foreach ($value as $name)
              {
                  $array[] = $name;
              }
          }
      }
      return $array;
    }
  
    function printArray()
    {
      print_r($this-&gt;nameArray);
    }
  }
  
  function getNewNames($name)
  {
      $array = array();
      switch ($name)
      {
          case "Mickey":
              $array[] = "Morty";
              $array[] = "Ferdie";
              break;
  
          case "Donald":
              $array[] = "Houie";
              $array[] = "Dewey";
              $array[] = "Louie";
              break;
  
          case "Goofy":
              $array[] = "Gilbert";
              break;
  
          case "Morty":
              $array[] = "Morty-B";
              break;
  
          case "Ferdie":
              $array[] = "Ferdie-B";
              break;
  
          case "Houie":
              $array[] = "Houie-B";
              break;
  
          case "Dewey":
              $array[] = "Dewey-B";
              break;
  
          case "Louie":
              $array[] = "Louie-B";
              break;
  
          case "Gilbert":
              $array[] = "Gilbert-B";
              break;
      }
  
      return $array;
  }
  
  $MAX_LEVELS = 3;
  $RPS = new RockPaperScissors();
  $RPS-&gt;addName(0, "Mickey");
  $RPS-&gt;addName(0, "Donald");
  $RPS-&gt;addName(0, "Goofy");
  
  $a = 0;
  $b = 0;
  for ($i=0; $i&lt;$MAX_LEVELS; $i++)
  {
      $namesFromRPS = $RPS-&gt;getNames($i);
      echo $i;
      foreach($namesFromRPS as $name)
      {
          echo $i;
          $newNames = getNewNames($name);
          foreach($newNames as $newName)
          {         
              echo $i;
              // try switching $i to $a or $b and notice the behaviour change of $i
              $RPS-&gt;addName($i, $newName);
          }
          $a++;
      }
      $b++;
  }
  
  //$RPS-&gt;printArray();
  </code></pre>