Possible Duplicate:
Strange behavior Of foreach
Strange behaviour after loop by reference - Is this a PHP bug?
Can anybody explain me why this code:
<pre>
<?php
$a = array('page', 'email', 'comment');
$b = array('page' => 'realpage', 'email' => 'reaLmail', 'comment' => 'c');
$c = array();
foreach ($a as &$item) {
if (isset($b[$item])) {
$item = $b[$item];
}
}
foreach ($a as $item) {
$c[] = $item;
}
print_r($c);
Outputs
Array
(
[0] => realpage
[1] => reaLmail
[2] => reaLmail
)
??? Why BEFORE second loop a is (by var_dump)
array(3) {
[0]=>
string(8) "realpage"
[1]=>
string(8) "reaLmail"
[2]=>
&string(1) "c"
}
But in first iteration, a is
array(3) {
[0]=>
string(8) "realpage"
[1]=>
string(8) "reaLmail"
[2]=>
&string(8) "realpage"
}
and on second and third [1] and [2] indexes are the same "reaLmail", and [2] is pointer? Thank you!