|  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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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: Sat Feb 24 23:01:27 2024 UTC