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

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 28 20:03:43 2021 UTC