Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:89998
Return-Path: <rowan.collins@gmail.com>
Mailing-List: contact internals-help@lists.php.net; run by ezmlm
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 50959 invoked from network); 4 Jan 2016 18:50:43 -0000
Received: from unknown (HELO lists.php.net) (127.0.0.1)
  by localhost with SMTP; 4 Jan 2016 18:50:43 -0000
Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass
Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass
Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.47 as permitted sender)
X-PHP-List-Original-Sender: rowan.collins@gmail.com
X-Host-Fingerprint: 74.125.82.47 mail-wm0-f47.google.com  
Received: from [74.125.82.47] ([74.125.82.47:33791] helo=mail-wm0-f47.google.com)
	by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP
	id 6F/87-07292-30FBA865 for <internals@lists.php.net>; Mon, 04 Jan 2016 13:50:43 -0500
Received: by mail-wm0-f47.google.com with SMTP id f206so1825255wmf.0
        for <internals@lists.php.net>; Mon, 04 Jan 2016 10:50:43 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=subject:to:references:from:message-id:date:user-agent:mime-version
         :in-reply-to:content-type:content-transfer-encoding;
        bh=mfhqHLvxKP3QWxXHVqT4BBP7qxuCitRs/ZO0h3E3poM=;
        b=D3MvxpB7DJGUebqjRgVxKRZw25cDAUix7EfIUT/GVL8XAGtAbaO42+M2JaPbFWrjS6
         i5frheAKHDwKtj4n+RLFHhjXKjZdi3I5axT37UMdQBjS9UFCtRom+yk1k8SXF3MBKtCP
         04D+L/QEzhzS8EwRvEpgGyedbWwtKTGQRDTp/uR29j5R9y6mwWcisHTg/b7qsyLQ213l
         Efl1e99bMkfY/dKooOQy6CZQYZBzAX/YYiU6G9Edhrp0h8g54Dpn+dYlK07Q2PNXxrSj
         dLg1CQ2tRry+us4CuNbtqQnbTO88c+9myGjaTG+ACCzhGsi+RT9GGSS0ciIVB9wuOc0G
         8i8w==
X-Received: by 10.194.246.134 with SMTP id xw6mr34310941wjc.158.1451933439827;
        Mon, 04 Jan 2016 10:50:39 -0800 (PST)
Received: from [192.168.1.189] ([2.31.24.87])
        by smtp.googlemail.com with ESMTPSA id u126sm79001092wme.3.2016.01.04.10.50.38
        for <internals@lists.php.net>
        (version=TLSv1/SSLv3 cipher=OTHER);
        Mon, 04 Jan 2016 10:50:39 -0800 (PST)
To: internals@lists.php.net
References: <070A1824-9ED7-4863-92AB-A4366AB92C98@zort.net>
 <BD.65.07292.E46AA865@pb1.pair.com>
Message-ID: <568ABEF8.7060705@gmail.com>
Date: Mon, 4 Jan 2016 18:50:32 +0000
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101
 Thunderbird/38.5.0
MIME-Version: 1.0
In-Reply-To: <BD.65.07292.E46AA865@pb1.pair.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Subject: Re: [PHP-DEV] Re: [RFC] array_key_(first|last|index) functions
 proposal
From: rowan.collins@gmail.com (Rowan Collins)

On 04/01/2016 17:05, Andrea Faulds wrote:
> How often would such functions be useful? Perhaps they fill a gap, but 
> I'm not sure if it's one that needs filling. array_key_first and 
> array_key_last can already be accomplished in two or so lines of code 
> (four if you make a function), and array_key_index can be implemented 
> in a few lines with a foreach() loop and a counter.

Personally, I think they would be very handy, if accompanied by 
array_value_first and array_value_last. The "ordered hash" PHP provides 
is a very powerful structure, but sometimes you just want to peek at one 
or other end of the array without any side effects, and it feels weird 
that there's no neat idiomatic way of doing this.

Incidentally, the ability to define the functions in userland is quite a 
positive - it means they can be polyfilled and codebases can start 
making use of them without requiring a major version bump.


> array_key_first and array_key_last seem mostly harmless, at least. I'm 
> not sure the same can be said for array_key_index, since it has O(n) 
> complexity. [...] I would rather we not include this specific 
> function, and avoid potentially disguising the time complexity of key 
> lookup, especially as I can't think of a good use-case for it.

This argument does seem reasonable - I can't think of many situations 
where you'd want exactly one key or element, at a known position, in an 
indexed array. And as soon as you want two keys or elements, you're 
probably better off using array_keys / array_values to give you a list 
which can be efficiently randomly accessed.

Regards,

-- 
Rowan Collins
[IMSoP]