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.
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.
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?
$arr = array(1=>'one',2=>'two',3=>'three');
foreach($arr as $k=>$v){
$v = &$arr[$k];
$v .= $k;
echo $v . "\n";
//unset($v) .... if I use unset($v) here, then the resulting $arr is correct.
}
output is...
one1
two2
three3
Array
(
[1] => two
[2] => three
[3] => three3
)
Thanks so much for the help!!