php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71240 array_filter byref value in callable no longer working
Submitted: 2015-12-29 20:05 UTC Modified: 2015-12-30 03:57 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: donatj at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.0.1 OS:
Private report: No CVE-ID: None
 [2015-12-29 20:05 UTC] donatj at gmail dot com
Description:
------------
It would appear that passing by reference in the callable of array_filter no longer functions as expected.  I can find no documentation of this change on the array_filter page.

The expected result is seen in 5.3 -> 5.6

Test script:
---------------
<?php

$files = array('AAA', 'BBB', 'CcC');

$files = array_filter($files, function ( &$path ) {
	$path = strtolower($path);
	return true;
});

print_r($files);

Expected result:
----------------
Array
(
    [0] => aaa
    [1] => bbb
    [2] => ccc
)

Actual result:
--------------
Array
(
    [0] => AAA
    [1] => BBB
    [2] => CcC
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-29 20:57 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-12-29 20:57 UTC] requinix@php.net
I don't see anything to suggest that was supported in the first place. There aren't even user comments doing that.

Besides, if you want to alter the array values then array_map() is more appropriate.
 [2015-12-29 20:58 UTC] nikic@php.net
-Status: Not a bug +Status: Verified
 [2015-12-29 20:58 UTC] nikic@php.net
@requinix: The issue here is that is does the by-reference modification, while it should not. A similar, but slightly different, bug exists in HHVM as well: https://3v4l.org/dp6Lt
 [2015-12-29 21:03 UTC] requinix@php.net
Fair enough. It just looked like one more of the various inconsistencies with references that was fixed with 7.
 [2015-12-29 22:22 UTC] nikic@php.net
-Status: Verified +Status: Not a bug
 [2015-12-29 22:22 UTC] nikic@php.net
@requinix: Actually I'm just stupid, you are totally right. I got confused with which output is which.
 [2015-12-29 22:39 UTC] donatj at gmail dot com
I still argue it's a bug. It's a behaviour that goes way back to at least 4.3.0 https://3v4l.org/iTqYI 

If nothing else, the change should be noted in the array_filter documentation, but I'd still argue it should be fixed.
 [2015-12-29 22:46 UTC] donatj at gmail dot com
Also the use case for this - I'm array_filter-ing already, not with a hardcoded true but actual logic, and as the code is already looping over items having a second loop through my array with array_map is silly.
 [2015-12-29 23:11 UTC] nikic@php.net
array_filter() is documented as taking the $array by value. It's not *allowed* to modify the original array in any way. Modifying the array violates basic semantics of by-value argument passing.
 [2015-12-30 03:57 UTC] yohgaki@php.net
Just an additional comment on this.
array_filter() is function filters array elements. To modify passed array itself, one should use array_walk()/array_walk_recursive()

http://php.net/manual/en/function.array-walk.php

I've seen this misuse on occasion. Nice to see this bug is fixed in PHP 7.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 14:01:31 2024 UTC