title: foreach and for loop in PHP
body: <p>am working on php loops and i find myself that i need to write a nested loop bur when i try to combine foreach and for loop it yield to unexpected result 
  Here are my codes</p>
  
  <pre><code>       foreach ($district_ward as $key =&gt; $value) {
             $ward_ids = array_keys($district_ward[$key]);
             echo $key;
             for ($x = 0; $x &lt; count($ward_ids)-1; $x++) {
                 $district_village[$key]= array_merge($value[$ward_ids[$x]], $value[$ward_ids[$x+1]]);
             }               
          }
  </code></pre>
  
  <p>This gives me this</p>
  
  <pre><code>347
  </code></pre>
  
  <p>but when i print the value of $key within the for loop, that is</p>
  
  <pre><code>        foreach ($district_ward as $key =&gt; $value) {
             $ward_ids = array_keys($district_ward[$key]);
             for ($x = 0; $x &lt; count($ward_ids)-1; $x++) {
     echo $key;
                 $district_village[$key]= array_merge($value[$ward_ids[$x]], $value[$ward_ids[$x+1]]);
             }
          }
  </code></pre>
  
  <p>i get this</p>
  
  <pre><code>3
  </code></pre>