Bug #67096 [Opn->Nab]: Iterators unexpectedly works with references

From: Date: Thu, 17 Apr 2014 20:42:07 +0000
Subject: Bug #67096 [Opn->Nab]: Iterators unexpectedly works with references
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185338@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67096&edit=1 ID: 67096 Updated by: rasmus@php.net Reported by: Azq2 at ya dot ru Summary: Iterators unexpectedly works with references -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Ubuntu 13.04 PHP Version: 5.5.11 Block user comment: N Private report: N New Comment: This is expected. If you expand the foreach you have the equivalent of: $k = 0; $res = &$array[0]; $v = 27112012; $k = 0; $v = &$res[0]; $k = 1; $v = &$res[1]; $k = 2; $v = &$res[2]; $k = 1; $res = &$array[1]; $v = 27112012; // But $v is a reference to $res[2] which is $array[0][2] // So this is like doing $array[0][2] = 27112012; $k = 0; $v = &$res[0]; $k = 0; $v = &$res[0]; $k = 1; $v = &$res[1]; $k = 2; $v = &$res[2]; $k = 2; $res = &$array[2]; $v = 27112012; // same here for $array[1][2] $k = 0; $v = &$res[0]; $k = 0; $v = &$res[0]; $k = 1; $v = &$res[1]; $k = 2; $v = &$res[2]; And since there are no further iterations nothing writes to $array[2][2] Previous Comments: ------------------------------------------------------------------------ [2014-04-17 20:38:25] Azq2 at ya dot ru Т.е. старое значение из $v по ссылке уходит в значение ключа $k массива $res. Value of array $res by key $k assigns by reference old value of $v. ------------------------------------------------------------------------ [2014-04-17 20:29:32] Azq2 at ya dot ru Description: ------------ Hello! I don't good speek english... i from Ukraine ;) Поэтому сразу напишу на русском. Суть бага: если переменная, которая используются в foreach как ссылка на результат текущей итерации, уже была декларирована ранее - то ей присваивается значение ранее декларированной переменной. (кроме последней) И портится массив, который используется в foreach. так не должно быть. if variable what used in foreach as reference to current iterator value declared before foreach - iterator value (expect last) assigns of before declared value. Test script: --------------- <?php $array = array( array(1, 1, 1), array(1, 1, 1), array(1, 1, 1), ); foreach ($array as $k => &$res) { $v = 27112012; foreach ($res as $k => &$v) { } } var_dump($array); Expected result: ---------------- array(3) { [0]=> array(3) { [0]=> int(1) [1]=> int(1) [2]=> int(1) } [1]=> array(3) { [0]=> int(1) [1]=> int(1) [2]=> int(1) } [2]=> array(3) { [0]=> int(1) [1]=> int(1) [2]=> int(1) } } Actual result: -------------- array(3) { [0]=> array(3) { [0]=> int(1) [1]=> int(1) [2]=> int(27112012) } [1]=> array(3) { [0]=> int(1) [1]=> int(1) [2]=> int(27112012) } [2]=> &array(3) { [0]=> int(1) [1]=> int(1) [2]=> &int(1) } } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=67096&edit=1

« previous php.bugs (#185338) next »