Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72355 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24633 invoked from network); 7 Feb 2014 00:26:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Feb 2014 00:26:14 -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.128.170 as permitted sender) X-PHP-List-Original-Sender: tjerk.meesters@gmail.com X-Host-Fingerprint: 209.85.128.170 mail-ve0-f170.google.com Received: from [209.85.128.170] ([209.85.128.170:37701] helo=mail-ve0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 85/00-24187-52824F25 for ; Thu, 06 Feb 2014 19:26:14 -0500 Received: by mail-ve0-f170.google.com with SMTP id cz12so2219421veb.1 for ; Thu, 06 Feb 2014 16:26:11 -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=UKl/qiM0bWcTsKtp90OWdsZcZzVml1N6c4C9pP+jNFI=; b=dBpPFRpi/1duDWdlhhwPu47BvxOukzkIM6ZqhPXAnoe5QK9U6t9U2XlLogYWVlkEMS 5mCbHWkjYYLBYaCBUdZdoX94ZPWHtpYQ/GX6af1y1O4X5oq/faOLKg9SHGUfZGykSWJT SGe2AURr34uzKgYQdSaRMHER924lNQxbk4eXwgbNC86smhXzgZaNMFHgJwUmDi/uucNS P/g6sQTIrCAbDo3IEEbtqPj+UcbKE58tnEWM2tgzfHVVaR4XP8nrVRB88Rbb1NhULgC8 d7UsS0RjR4rk7zQW9i6VxB3Ih1h3IFAVKUreadl3PwAeouaDbkh1/8ujCqbs1MIdnlSj KK2Q== MIME-Version: 1.0 X-Received: by 10.52.243.102 with SMTP id wx6mr6650036vdc.12.1391732771148; Thu, 06 Feb 2014 16:26:11 -0800 (PST) Received: by 10.58.133.229 with HTTP; Thu, 6 Feb 2014 16:26:11 -0800 (PST) In-Reply-To: References: Date: Fri, 7 Feb 2014 08:26:11 +0800 Message-ID: To: Chris Wright Cc: "Kingsquare.nl - Robin Speekenbrink" , PHP Internals Content-Type: multipart/alternative; boundary=001a1133ec96725c9104f1c60956 Subject: Re: [PHP-DEV] [discussion] add array_usearch() From: tjerk.meesters@gmail.com (Tjerk Meesters) --001a1133ec96725c9104f1c60956 Content-Type: text/plain; charset=ISO-8859-1 Hi Chris, On Fri, Feb 7, 2014 at 4:28 AM, Chris Wright wrote: > On 6 February 2014 19:55, Tjerk Meesters wrote: > > 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'm not sure where you got this statement from. array_search() is > still O(n) in the worst case (when the needle is not found), which is > the best you can hope for with such an operation. It's true that the > other two are *always* O(n) whereas array_search() *could* return > sooner than a complete iteration if the needle is found before the > last element. > I was talking about memory growth, not runtime :) > > My initial reaction to this is one of "ye gods, not another array_* > function", but I wouldn't be against altering the existing > array_search() so that the third parameter is a bitmask of flags, one > of which could indicate that the search should be case insensitive. > This could be done in a BC-compliant manner if the flag that indicates > strict comparisons has a value of 1. > I'm not wildly enthusiastic about another magic flags parameter, but it's an acceptable compromise for me. The slight advantage there is that `in_array()` would be bestowed upon the ability to take a callback function as well. Something like: if (in_array('is_numeric', $array, SEARCH_NEEDLE_IS_CALLBACK)) { ... } > > Thanks, Chris > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- -- Tjerk --001a1133ec96725c9104f1c60956--