Re: [discussion] add array_usearch()

From: Date: Thu, 06 Feb 2014 19:55:09 +0000
Subject: Re: [discussion] add array_usearch()
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-72346@lists.php.net to get a copy of this message
Hi Robin, On Thu, Feb 6, 2014 at 11:53 PM, Kingsquare.nl - Robin Speekenbrink < robin@kingsquare.nl> wrote: > 2014-02-06 Tjerk Meesters <tjerk.meesters@gmail.com>: > > 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]. > > > > > > Tjerk, > > This feels alot like array_filter.. Do we really want to introduce yet > another way of doing almost the same? > Consider the following examples for finding a value in an array: array_search($value, $array, true); current(array_keys($array, $value, true)); key(array_filter($array, function($item) use ($value) { return $item === $value; })); Perhaps I've overlooked one way, but that's already three ways of doing something similar; the difference lies in the memory behaviour, the first being O(1) whereas the other two are O(n). I initially wanted to extend array_search() to support a Closure as the $needle argument, but that would lead to some BC issues. > > Regards, > > Robin Speekenbrink > -- -- Tjerk

« previous php.internals (#72346) next »