Understanding foreach logic with references - Why is the 1st element being changed to 'two', the 2nd to 'three', and the third to 'three3'?
<p>I am trying to understand the ins and outs of how references work, but I got stuck trying to follow the programming logic of what exactly this code is doing as it loops through the array. I'm trying to understand what is happening, step by step. </p>

<p>I have read through the PHP.net posts, and understand that when using references in the foreach of an array you have to unset the variable. I also understand that the code below is not the best code. I'm just using it for learning purposes to follow programming flow logic on how the php interpreter runs through different code.</p>

<p>My question is, what is this code doing each time it loops through this array, to cause it to output the following if I do not unset($v)? In other words, what is it doing step by step to get the array to have 'two' as the first element, 'three', as the second element, and 'three3' as the third?</p>

<pre><code>$arr = array(1=&gt;'one',2=&gt;'two',3=&gt;'three');

foreach($arr as $k=&gt;$v){

   $v = &amp;$arr[$k];
   $v .= $k;
   echo $v . "\n";
   //unset($v) .... if I use unset($v) here, then the resulting $arr is correct.
}
</code></pre>

<p>output is...</p>

<pre><code>one1
two2
three3

Array
(
[1] =&gt; two
[2] =&gt; three
[3] =&gt; three3
)
</code></pre>

<p>Thanks so much for the help!!</p>
