Bug #74038 [Com]: Arrays unchanged when value reference is modified in an array_filter(), etc

From: Date: Fri, 03 Feb 2017 02:24:17 +0000
Subject: Bug #74038 [Com]: Arrays unchanged when value reference is modified in an array_filter(), etc
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207139@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74038&edit=1

 ID:                 74038
 Comment by:         Rican7 at gmail dot com
 Reported by:        Rican7 at gmail dot com
 Summary:            Arrays unchanged when value reference is modified in
                     an array_filter(), etc
 Status:             Open
 Type:               Bug
 Package:            Arrays related
 PHP Version:        7.0.15
 Block user comment: N
 Private report:     N

 New Comment:

Ah, this seems to be related to (if not the same issue as) #71240: https://bugs.php.net/bug.php?id=71240


Previous Comments:
------------------------------------------------------------------------
[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)

« previous php.bugs (#207139) next »