|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54739 array_filter: optional parameter for max_items
Submitted: 2011-05-15 22:48 UTC Modified: 2013-07-18 07:54 UTC
Avg. Score:3.5 ± 0.5
Reproduced:0 of 0 (0.0%)
From: ajcoon at gmail dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: 5.3.6 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: ajcoon at gmail dot com
New email:
PHP Version: OS:


 [2011-05-15 22:48 UTC] ajcoon at gmail dot com
Per this discussion on StackOverflow, I would like to recommend an additional, optional parameter be supported in `array_filter`.  The parameter, named `max_items` (or similar) would be expected as an integer value >0, and used to return a max of `n` matching items in the search array.  This is useful in the case where sort order is non-deterministic and the number of desired items is low relative to the length of the search array.

Related discussion:

Test script:
$objects = array_filter($myArray, function($item){ return $item->id == 91 }, 1);

Expected result:
Expect `array_filter` to stop searching for additional matches once it reaches `max_items` number of matches.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-18 07:54 UTC]
-Status: Open +Status: Wont fix
 [2013-07-18 07:54 UTC]
So this is a hint to increase performance. You're probably much better off when filtering much closer by the logic which gathers the data (i.e. ORDER BY .. LIMIT).

Also, some sort of slipper slope, one might argue a (sometimes micro) optimization when adding this parameter to a lot of array functions, and I don't think it all added parameters, documentation and edge cases are worth it.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 15 17:01:30 2024 UTC