Bug #76349 [Opn->Csd]: Spoils the original array

From: Date: Wed, 16 May 2018 19:50:04 +0000
Subject: Bug #76349 [Opn->Csd]: Spoils the original array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-215290@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76349&edit=1 ID: 76349 Updated by: peehaa@php.net Reported by: arnowt at gmail dot com Summary: Spoils the original array -Status: Open +Status: Closed Type: Bug Package: *General Issues Operating System: Linux PHP Version: Irrelevant -Assigned To: +Assigned To: peehaa Block user comment: N Private report: N New Comment: Duplicate of https://bugs.php.net/bug.php?id=76336 and many others Previous Comments: ------------------------------------------------------------------------ [2018-05-16 19:27:53] arnowt at gmail dot com Я знаю что такое ссылки и использую их по назначению. Просто упростил пример. В коде нет не единого присваивания, однако исходный массив изменяется. Понятно что баг можно обойти, например если оба раза использовать ссылку(&$v) пример: $res=[1,2,3]; foreach($res as &$v){} print_r($res); foreach($res as &$v){} print_r($res); ------------------------------- Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 //теперь все ожидаемо ) ------------------------------------------------------------------------ [2018-05-16 19:14:37] spam2 at rhsoft dot net to make it clear: you miss a unset between both loops, $v of the second one still points to the last item of the previous loop ------------------------------------------------------------------------ [2018-05-16 19:11:39] spam2 at rhsoft dot net just don't use references until you understand their implications and you are really sure you gain something - given the php engine has a copy-on-write paradigm there are very few cases ------------------------------------------------------------------------ [2018-05-16 18:24:27] arnowt at gmail dot com Description: ------------ Портит исходный массив. Проверял на версии v5.6 и v7.2 Test script: --------------- $res=[1,2,3]; foreach($res as &$v){} print_r($res); foreach($res as $v){} print_r($res); Expected result: ---------------- Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 ) Actual result: -------------- Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 2 //!!!!!! ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76349&edit=1

« previous php.bugs (#215290) next »