Bug #71969 [Opn->Csd]: str_replace returns an incorrect resulting array after a foreach by reference

From: Date: Wed, 06 Apr 2016 02:21:37 +0000
Subject: Bug #71969 [Opn->Csd]: str_replace returns an incorrect resulting array after a foreach by reference
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200414@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71969&edit=1 ID: 71969 Updated by: laruence@php.net Reported by: vad at bitrix dot ru Summary: str_replace returns an incorrect resulting array after a foreach by reference -Status: Open +Status: Closed Type: Bug Package: Arrays related Operating System: CentOS PHP Version: 7.0.5 Block user comment: N Private report: N New Comment: Automatic comment on behalf of laruence@gmail.com Revision: http://git.php.net/?p=php-src.git;a=commit;h=e95782ed5e26c041d62b847dc772f86fe6b01516 Log: Fixed bug #71969 (str_replace returns an incorrect resulting array after a foreach by reference) Previous Comments: ------------------------------------------------------------------------ [2016-04-05 17:22:18] vad at bitrix dot ru Description: ------------ After foreach by reference, with the array value changing, the array is passed to the str_replace() as a subject. It's expected to get the array back unchanged, but it's broken with a "Notice: Array to string conversion". This is only PHP 7 related, earlier versions are OK. https://3v4l.org/iOlfo Test script: --------------- $a = array( array("one" => array("a"=>"0000", "b"=>"1111")), ); //foreach by reference, changing the array value foreach($a as &$record) { $record["one"]["a"] = "2222"; } var_dump($a); var_dump(str_replace("2", "3", $a)); Expected result: ---------------- array(1) { [0]=> &array(1) { ["one"]=> array(2) { ["a"]=> string(4) "2222" ["b"]=> string(4) "1111" } } } array(1) { [0]=> array(1) { ["one"]=> array(2) { ["a"]=> string(4) "2222" ["b"]=> string(4) "1111" } } } Actual result: -------------- array(1) { [0]=> &array(1) { ["one"]=> array(2) { ["a"]=> string(4) "2222" ["b"]=> string(4) "1111" } } } Notice: Array to string conversion in /in/iOlfo on line 14 array(1) { [0]=> string(5) "Array" } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71969&edit=1

« previous php.bugs (#200414) next »