php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55633 New argument $invert for array_filter()
Submitted: 2011-09-07 11:58 UTC Modified: 2021-09-09 14:46 UTC
Votes:5
Avg. Score:3.0 ± 1.1
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: vovan-ve at yandex dot ru Assigned:
Status: Suspended Package: Arrays related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vovan-ve at yandex dot ru
New email:
PHP Version: OS:

 

 [2011-09-07 11:58 UTC] 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) ""
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-23 22:39 UTC] 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);
}));
 [2012-01-23 23:44 UTC] vovan-ve at yandex dot ru
> 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.
 [2012-08-21 02:51 UTC] vovan-ve at yandex dot ru
Related RFC was created: "array_delete() for elements deletion" - https://wiki.php.net/rfc/array_delete
 [2021-09-09 14:46 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-09-09 14:46 UTC] cmb@php.net
> Related RFC was created: […]

And is inactive for many years.  Anybody still interested in this
feature is welcome to pursue the RFC process[1].  For the time
being, I suspend this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 06:01:27 2025 UTC