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

From: Date: Fri, 03 Feb 2017 01:51:53 +0000
Subject: Bug #74038 [NEW]: Arrays unchanged when value reference is modified in an array_filter(), etc
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207138@lists.php.net to get a copy of this message
From:             Rican7 at gmail dot com
Operating system: 
PHP version:      7.0.15
Package:          Arrays related
Bug Type:         Bug
Bug description:Arrays unchanged when value reference is modified in an array_filter(), etc

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 bug report at https://bugs.php.net/bug.php?id=74038&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=74038&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=74038&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=74038&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=74038&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=74038&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=74038&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=74038&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=74038&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=74038&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=74038&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=74038&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=74038&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=74038&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74038&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=74038&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=74038&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=74038&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74038&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=74038&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=74038&r=mysqlcfg



Thread (5 messages)

« previous php.bugs (#207138) next »