Req #78176 [NEW]: array_search() should accept a callable as $needle

From: Date: Mon, 17 Jun 2019 17:12:37 +0000
Subject: Req #78176 [NEW]: array_search() should accept a callable as $needle
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-221350@lists.php.net to get a copy of this message
From: david at davidwbarratt dot com Operating system: PHP version: Next Minor Version Package: *General Issues Bug Type: Feature/Change Request Bug description:array_search() should accept a callable as $needle 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 ); -- Edit bug report at https://bugs.php.net/bug.php?id=78176&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=78176&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=78176&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=78176&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=78176&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=78176&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=78176&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=78176&r=needscript Try newer version: https://bugs.php.net/fix.php?id=78176&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=78176&r=support Expected behavior: https://bugs.php.net/fix.php?id=78176&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=78176&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=78176&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=78176&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=78176&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=78176&r=dst IIS Stability: https://bugs.php.net/fix.php?id=78176&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=78176&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=78176&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=78176&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=78176&r=mysqlcfg

« previous php.bugs (#221350) next »