php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78176 New function array_search_callback() accepts a callable as $needle
Submitted: 2019-06-17 17:12 UTC Modified: 2021-06-08 14:44 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: david at davidwbarratt dot com Assigned:
Status: Suspended Package: Arrays related
PHP Version: Next Minor Version 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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: david at davidwbarratt dot com
New email:
PHP Version: OS:

 

 [2019-06-17 17:12 UTC] david at davidwbarratt dot com
Description:
------------
It would be really helpful if array_search() would accept a callable as $needle. There are many instances where it is needed to loop through an array and stop and the first instance. Currently, the only work around is to make a function with a foreach() that returns early. This would bring PHP inline with JavaScript's Array.find() method.

This change would not be a breaking change because array_search() would continue to work exactly as before the change. The only difference would be that now $needle can accept a callable that returns a boolean.

This change would also make array_search() more like array_filter().

Test script:
---------------
// Current
function search( $haystack ) {
	foreach ( $haystack as $index => $value ) {
		if ( $value->id === 3 ) {
			return $value;
		}
	}

	return null;
}
$found = search( $haystack );

// Future
$found = array_search( function( $value, $index ) {
	return $value->id === 3;
}, $haystack );


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-18 01:58 UTC] requinix@php.net
-Summary: array_search() should accept a callable as $needle +Summary: New function array_search_callback() accepts a callable as $needle -Package: *General Issues +Package: Arrays related
 [2019-06-18 01:58 UTC] requinix@php.net
It would be a breaking change: callables can be strings, arrays, __invoke-able objects, and Closures. There will be code out there that has arrays of those, and if array_search thinks its $needle may be callable then searching those arrays won't work.

So this should be a new function. Probably "array_search_callback", like preg_replace_callback.
 [2019-06-18 12:54 UTC] david at davidwbarratt dot com
Ah! I didn't think about callable arrays.

May I suggest using the name array_search() instead? None of the array functions have _callback in them, so I think it might be odd to add that.
 [2019-06-18 12:56 UTC] david at davidwbarratt dot com
Oops... and by array_search() I mean array_find() (sorry for the confusion!). array_find() would also be similar in naming to Array.find() in JavaScript.
 [2019-06-18 13:09 UTC] requinix@php.net
I foresee a lot of "array_search or array_find, which does which?" confusion.

Anyway, if you'd like to help this along, in whatever form, check out the internals mailing list.
https://www.php.net/mailing-lists.php
 [2021-06-08 14:44 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-06-08 14:44 UTC] cmb@php.net
Such feature additions require the RFC process[1].  Anybody is
welcome to pursue it.  For the time being, I suspend this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC