Bug #74038 [Opn->Dup]: Arrays unchanged when value reference is modified in an array_filter(), etc
Edit report at https://bugs.php.net/bug.php?id=74038&edit=1
ID: 74038
Updated by: requinix@php.net
Reported by: Rican7 at gmail dot com
Summary: Arrays unchanged when value reference is modified in
an array_filter(), etc
-Status: Open
+Status: Duplicate
Type: Bug
Package: Arrays related
PHP Version: 7.0.15
Block user comment: N
Private report: N
New Comment:
Yeah, same basic issue. Those functions never claimed you could modify the values inside the
callbacks, so the fact that you could in PHP 5 was a quirk.
Previous Comments:
------------------------------------------------------------------------
[2017-02-03 02:24:17] Rican7 at gmail dot com
Ah, this seems to be related to (if not the same issue as) #71240: https://bugs.php.net/bug.php?id=71240
------------------------------------------------------------------------
[2017-02-03 01:51:52] Rican7 at gmail dot com
Description:
------------
I just updated an application from PHP 5.6 to PHP 7.0 after carefully reading the upgrading guide
and documentation, but I just ran into a bit of an issue.
We had code that was modifying values (via a value reference) in place when passed to an
array_filter() call, to prevent us from having to run multiple loop iterations, but
that doesn't seem to work in PHP 7.
While I understand that the runtime semantics may have changed for these functions, I can't
find this behavior change to be documented anywhere, so I wasn't sure if it was intentional or
not. I know that foreach has changed in PHP 7, which is relatively well documented, but
these array function behavior changes don't seem to be.
I've tried to see if this behavior has changed in other array functions too, such as
array_reduce, and the issue seems to exist there too. My guess is that this has to do
with how values are passed/referenced in these functions under the new PHP 7 runtime/engine.
Test script:
---------------
/**
* 3v4l.org URL: https://3v4l.org/bERrp
*/
$data = ['foo', 'bar'];
var_dump($data);
$filtered = array_filter($data, function (&$value) {
$value = (bool) $value;
return true;
});
var_dump($filtered);
Expected result:
----------------
array(2) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
}
array(2) {
[0]=>
bool(true)
[1]=>
bool(true)
}
Actual result:
--------------
array(2) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
}
array(2) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74038&edit=1
Thread (5 messages)