|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-08-28 12:11 UTC] kernins at gmail dot com
Description:
------------
Passing array element to callback by ref doesn't work anymore, resulting array contains original/unmodified values
Test script:
---------------
$arr=['foo', 'bar', '', 'baz'];
$arr=array_filter($arr, function(&$el){
$el=strtoupper($el);
return strlen($el)>0;
});
var_dump($arr);
Expected result:
----------------
array(3) {
[0]=>
string(3) "FOO"
[1]=>
string(3) "BAR"
[3]=>
string(3) "BAZ"
}
Actual result:
--------------
array(3) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[3]=>
string(3) "baz"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 12 10:00:01 2025 UTC |
> First of all, it worked the whole 5.x series and I have a lot of code using it that way. Such usage was considered valid before => BC break here. Sure. It's a BC break in a semver-major release fixing a bug in the previous implementation. > This will result in two loops instead of one => performance hit on large arrays (even if it is negligible, it is still here) and less compact code. If you are interested in performance, using array_filter() and similar is already a losing proposition. The most efficient way to perform any kind of loop-based operation is a foreach() loop. People use functions like array_map() or array_filter() not for reasons of performance, but for reasons of clarity. It goes without saying that the ability to perform modifications inside a function called "filter", which is a very well-defined functional programming construct that certainly does not include mutability, somewhat defeats the purpose of having a well-delineated functional processing pipeline. $arr = array_map('strtoupper', array_filter($arr, 'strlen')); > Why the hell one array_* functions are working with refs, while the others shouldn't? This at least adds even more inconsistency to already inconsistent php api. Because some functions operate in-place, while others return new arrays. Functions operating in-place (array_walk, sort, shuffle) accept by-reference, while nearly all other functions (array_map, array_filter) do not operate in-place and accept by-value.