[discussion] add array_usearch()

From: Date: Thu, 06 Feb 2014 14:38:51 +0000
Subject: [discussion] add array_usearch()
Groups: php.internals 
Request: Send a blank email to internals+get-72342@lists.php.net to get a copy of this message
Hi internals, To complement array_search(), I'm gauging the interest in adding the following function: mixed array_usearch(array $haystack, callable $fn, int $flags = 0) It returns the first array key from a given haystack for which the callback function returns a truthy value [1] or false otherwise. The callback function receives the array item as its only argument [2]. The flags, described below, can be combined by using a binary or. [1] the interpretation of the return value can be inverted by using ARRAY_USEARCH_INVERT [2] the array key is passed as the 2nd argument by using ARRAY_USEARCH_USE_KEY An implementation of array_all() and array_some(), which you may find in ECMAScript: function array_some(array $array, $fn) { return array_usearch($array, $fn) !== false; } function array_all(array $array, $fn) { return array_usearch($array, $fn, ARRAY_USEARCH_INVERT) === false; } Another example: echo array_usearch(['a', 'b', 2], 'is_numeric'); // 2 The implementation can be found here: https://github.com/datibbaw/php-src/compare/master...array-usearch Let me know your thoughts. -- -- Tjerk

« previous php.internals (#72342) next »