Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72346 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2770 invoked from network); 6 Feb 2014 19:55:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Feb 2014 19:55:13 -0000 Authentication-Results: pb1.pair.com smtp.mail=tjerk.meesters@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tjerk.meesters@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.50 as permitted sender) X-PHP-List-Original-Sender: tjerk.meesters@gmail.com X-Host-Fingerprint: 209.85.212.50 mail-vb0-f50.google.com Received: from [209.85.212.50] ([209.85.212.50:61012] helo=mail-vb0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6E/50-31659-1A8E3F25 for ; Thu, 06 Feb 2014 14:55:13 -0500 Received: by mail-vb0-f50.google.com with SMTP id w8so1865970vbj.9 for ; Thu, 06 Feb 2014 11:55:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=RybASRFvgsbUW87VRpFDr1Zxrw7vZOMCGYB1uZT2Glw=; b=hvh5smF99AJaV5yCHxKnQpZAtus5W1CEAyj/6v8RflPB5Zs/034vHs+fzdgWVFH4pJ CaBC+qDV2kpUE2P/Q9qolYSAO7Z8kIlPb+7O4kNbxBCTP62B1Ng410X3salwSv8FHqB0 YaTi07W1Ax6TdRsdSSQWwkf9QwEB+Tj/5jyrihzUgdPPf25nsnjxDEAnsz1lE+kzk0J5 rqGZm/mRvw+FFwROptlF24ar8cPVSz0dX7Khlpr+2NLyZD72jHTl8hPuL4dnyC5/b8Ta sgxsvtCwdykzFR9GC/JfYJVLnie8gzhx0/PZoFSXvYYtGQK0uTt3II6GRXN68T1OYtkk qKDQ== MIME-Version: 1.0 X-Received: by 10.58.255.233 with SMTP id at9mr7289133ved.20.1391716509525; Thu, 06 Feb 2014 11:55:09 -0800 (PST) Received: by 10.58.133.229 with HTTP; Thu, 6 Feb 2014 11:55:09 -0800 (PST) In-Reply-To: References: Date: Fri, 7 Feb 2014 03:55:09 +0800 Message-ID: To: "Kingsquare.nl - Robin Speekenbrink" Cc: PHP Internals Content-Type: multipart/alternative; boundary=047d7bf15fc82db05c04f1c2403f Subject: Re: [PHP-DEV] [discussion] add array_usearch() From: tjerk.meesters@gmail.com (Tjerk Meesters) --047d7bf15fc82db05c04f1c2403f Content-Type: text/plain; charset=ISO-8859-1 Hi Robin, On Thu, Feb 6, 2014 at 11:53 PM, Kingsquare.nl - Robin Speekenbrink < robin@kingsquare.nl> wrote: > 2014-02-06 Tjerk Meesters : > > 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 --047d7bf15fc82db05c04f1c2403f--