Bug #71969 [NEW]: str_replace returns an incorrect resulting array after a foreach by reference
| From: | vad at bitrix dot ru | Date: | Tue, 05 Apr 2016 17:22:23 +0000 |
| Subject: | Bug #71969 [NEW]: str_replace returns an incorrect resulting array after a foreach by reference | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-200420@lists.php.net to get a copy of this message | ||
From: vad at bitrix dot ru
Operating system: CentOS
PHP version: 7.0.5
Package: Arrays related
Bug Type: Bug
Bug description:str_replace returns an incorrect resulting array after a foreach by reference
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 bug report at https://bugs.php.net/bug.php?id=71969&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71969&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71969&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71969&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71969&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71969&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71969&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71969&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71969&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71969&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71969&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71969&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71969&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71969&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71969&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71969&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71969&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71969&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71969&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71969&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71969&r=mysqlcfg