php.net |  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
Votes:2
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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ajcoon at gmail dot com
New email:
PHP Version: OS:

 

 [2011-05-15 22:48 UTC] ajcoon at gmail dot com
Description:
------------
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:

http://stackoverflow.com/questions/6010872/getting-a-value-from-an-array-in-php

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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-18 07:54 UTC] maarten@php.net
-Status: Open +Status: Wont fix
 [2013-07-18 07:54 UTC] maarten@php.net
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 Dec 21 12:01:31 2024 UTC