|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[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
[2019-06-18 12:54 UTC] david at davidwbarratt dot com
[2019-06-18 12:56 UTC] david at davidwbarratt dot com
[2019-06-18 13:09 UTC] requinix@php.net
[2021-06-08 14:44 UTC] cmb@php.net
-Status: Open
+Status: Suspended
[2021-06-08 14:44 UTC] cmb@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 00:00:02 2025 UTC |
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 );