Re: PR 287 - added use_keys argument to array_filter() [Discussion]

From: Date: Tue, 18 Jun 2013 17:04:20 +0000
Subject: Re: PR 287 - added use_keys argument to array_filter() [Discussion]
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
> var_dump(array_filter(['foo', '', 'bar'], 'strlen',
> true));
>
> Warning: strlen() expects exactly 1 parameter, 2 given in - on line 1
>
> Not only do we trigger the error handler (a large enough array causes a
> performance issue), but we also get back an empty array as a result.
That's
> BC and performance loss that's simply unacceptable, so defaulting the
> behavior to pass the key is simply out of the question.

Existing code won't pass that third, true, argument to array_filter(), and
thus will not break.

Nevertheless, I'd say go with a new array_filter_key() function, because
that permits using existing one-parameter functions like that strlen as a
callback, instead of forcing two-parameter callback functions.

Another alternative might be to go with a third argument to array_filter(),
but make that an integer with ORable constants ARRAY_FILTER_KEY,
ARRAY_FILTER_VAL - only one of them set calls with a single argument, both
set call with two arguments.

best regards
  Patrick


Thread (39 messages)

« previous php.internals (#67725) next »