Edit report at https://bugs.php.net/bug.php?id=55633&edit=1

 ID:                 55633
 Comment by:         vovan-ve at yandex dot ru
 Reported by:        vovan-ve at yandex dot ru
 Summary:            New argument $invert for array_filter()
 Status:             Open
 Type:               Feature/Change Request
 Package:            Arrays related
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

> Use Closures

Usage of any user-defined function as a callback anywhere will reduce 
performance. I think, it is much better to improve array_filter() as mentioned 
above.


Previous Comments:
------------------------------------------------------------------------
[2012-01-23 22:39:24] gmblar+php at gmail dot com

Use Closures

$values = array(42, 'foo', false, null, array(), '');
var_dump(array_filter($values, function($value) {
    return !is_null($value);
}));

------------------------------------------------------------------------
[2011-09-07 11:58:51] vovan-ve at yandex dot ru

Description:
------------
I think, it would be useful to add new argument $invert to array_filter() 
function. It will be most useful to use built-in functions as a callback. For 
example this call:

    $result = array_filter($values, 'is_null', true);

should to _remove_ all NULLs from array.

May be the parameter should be named $positive and have default value TRUE (and 
FALSE for inversion) to make code readable.

Test script:
---------------
$values = array(42, 'foo', false, null, array(), '');
var_dump(
  array_filter($values, null, true), // no callback - leave false value
  array_filter($values, 'is_null', true) // leave non-null values
);

Expected result:
----------------
array(5) {
  [0]=>
  bool(false)
  [1]=>
  NULL
  [2]=>
  array(0) {
  }
  [3]=>
  string(0) ""
}
array(5) {
  [0]=>
  int(42)
  [1]=>
  string(3) "foo"
  [2]=>
  bool(false)
  [3]=>
  array(0) {
  }
  [4]=>
  string(0) ""
}



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=55633&edit=1

Reply via email to