php - Strange foreach loop after modifying array -
as wrote code, php confused me little didn't expected result of following code:
$data = array(array('test' => 'one'), array('test' => 'two')); foreach($data &$entry) { $entry['test'] .= '+'; } foreach($data $entry) { echo $entry['test']."\n"; }
i think should output
one+ two+
however result is: http://ideone.com/e5tcsi
one+ one+
can explain me why?
this expected behaviour, see https://bugs.php.net/bug.php?id=29992.
the reference maintained when using second foreach, when using second foreach value of $entry
, points still $data[1]
, overwritten first value.
p.s. (thanks @billyonecan saying it): need unset($entry)
first, reference destroyed.
Comments
Post a Comment