title: php issue with looping over an array twice using foreach and passing value by reference
body: <p>I need to loop through an array twice - once to modify its values, and once to display its content in html. Unfortunately, I am running into trouble. I have created a test scenario to illustrate the issue.</p>
  
  <pre><code>$cases = array(
    array('caseStyle' =&gt; 'case style 1', 'caseNum' =&gt; 'case01'),
    array('caseStyle' =&gt; 'case style 2', 'caseNum' =&gt; 'case02'),
    array('caseStyle' =&gt; 'case style 3', 'caseNum' =&gt; 'case03'),
    array('caseStyle' =&gt; 'case style 4', 'caseNum' =&gt; 'case04'),
    array('caseStyle' =&gt; 'case style 5', 'caseNum' =&gt; 'case05'),
    array('caseStyle' =&gt; 'case style 6', 'caseNum' =&gt; 'case06')
  );
  
  foreach ($cases as $k =&gt; &amp;$v) {
      $v['caseNum'] = ucwords($v['caseNum']);
  }
  
  foreach ($cases as $k =&gt; $v) {
      echo $v['caseNum'] . ' - ' . $v['caseStyle'] . '&lt;br/&gt;';
  }
  </code></pre>
  
  <p>This outputs:</p>
  
  <pre><code>Case01 - case style 1
  Case02 - case style 2
  Case03 - case style 3
  Case04 - case style 4
  Case05 - case style 5
  Case05 - case style 5
  </code></pre>
  
  <p>Note the values for the last item are wrong.</p>
  
  <p>If during the second iteration I use 
  <code>foreach($cases as $k =&gt; $d)</code> instead of <code>foreach($cases as $k =&gt; $v)</code> or if I unset <code>$v</code> before the second iteration <code>(unset($v))</code> everything goes fine. This is intriguing. What am I missing?</p>