Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:89994
Return-Path: <ajf@ajf.me>
Mailing-List: contact internals-help@lists.php.net; run by ezmlm
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 39154 invoked from network); 4 Jan 2016 17:05:19 -0000
Received: from unknown (HELO lists.php.net) (127.0.0.1)
  by localhost with SMTP; 4 Jan 2016 17:05:19 -0000
X-Host-Fingerprint: 2.218.134.247 unknown  
Received: from [2.218.134.247] ([2.218.134.247:9223] helo=localhost.localdomain)
	by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP
	id BD/65-07292-E46AA865 for <internals@lists.php.net>; Mon, 04 Jan 2016 12:05:19 -0500
Message-ID: <BD.65.07292.E46AA865@pb1.pair.com>
To: internals@lists.php.net
References: <070A1824-9ED7-4863-92AB-A4366AB92C98@zort.net>
Date: Mon, 4 Jan 2016 17:05:13 +0000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:42.0)
 Gecko/20100101 Firefox/42.0 SeaMonkey/2.39
MIME-Version: 1.0
In-Reply-To: <070A1824-9ED7-4863-92AB-A4366AB92C98@zort.net>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Posted-By: 2.218.134.247
Subject: Re: [RFC] array_key_(first|last|index) functions proposal
From: ajf@ajf.me (Andrea Faulds)

Hi John,

John Bafford wrote:
> Happy New Year, everyone!
>
> I’d like to present the first new PHP RFC for this year, a proposal to add functions to easily get the first, last, or an arbitrary key (and value) by index from an array, taking advantage of PHP’s property that arrays are ordered maps.
>
> RFC: https://wiki.php.net/rfc/array_key_first_last_index
> PR: https://github.com/php/php-src/pull/347

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.

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 worry that it might end up used by people who think it is 
more efficient than iterating through an array, even though it is not. 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. As for array_key_first and 
array_key_last, well, maybe they're useful, I have no strong opinion on 
them. They might be handy to get the first and last key of an array 
without moving the internal pointer, so who knows, maybe we should add them.

Thanks.
-- 
Andrea Faulds
https://ajf.me/