Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85330 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24885 invoked from network); 20 Mar 2015 21:04:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2015 21:04:56 -0000 Authentication-Results: pb1.pair.com header.from=jbafford@zort.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jbafford@zort.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zort.net designates 96.241.205.2 as permitted sender) X-PHP-List-Original-Sender: jbafford@zort.net X-Host-Fingerprint: 96.241.205.2 nova.zort.net Received: from [96.241.205.2] ([96.241.205.2:40145] helo=nova.zort.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 01/4A-64120-77B8C055 for ; Fri, 20 Mar 2015 16:04:56 -0500 Received: from [10.0.1.2] (pulsar.zort.net [96.241.205.6]) (authenticated bits=0) by nova.zort.net (8.14.5/8.14.5) with ESMTP id t2KL4p9P011089 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Fri, 20 Mar 2015 17:04:51 -0400 Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) In-Reply-To: <550C8879.70002@gmail.com> Date: Fri, 20 Mar 2015 17:04:50 -0400 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: References: <550B5A81.1090706@gmail.com> <550C8879.70002@gmail.com> To: Stanislav Malyshev X-Mailer: Apple Mail (2.1878.6) Subject: Re: [PHP-DEV] RFC - Array slice syntactic sugar From: jbafford@zort.net (John Bafford) On Mar 20, 2015, at 16:52, Stanislav Malyshev = wrote: > Hi! >=20 >>> My proposal is something similar to Pythons slice, in PHP this would = look >>> like: >>>=20 >>> $slided =3D $array[1:4] >>>=20 >>> This will get the elements in positions 1,2,3,4. (1 through 4 = inclusive), >>> ignoring the actual key of the array. The result for an array will = be an >>> array with the keys preserved, in the same order. >=20 > I'm not sure how such operation would be useful, and it definitely = would > not be intuitive, as $array[0] and $array[0:1] (assuming non-inclusive > semantic, or [0:0] with inclusive semantics) would return completely > different things. That would happen even if the array has only numeric > keys! This is the main problem with this syntax - unlike most = languages > where it is used, PHP arrays are not vectors, they are ordered = hashmaps. > Sometimes this is very convenient, sometimes - like for this syntax - = it > is not. I think this is the major reason why such proposals failed in > the past. My interest in this proposal is mostly because it can potentially = provide a convenient way to get the first element in an array, which is = a not-infrequent need of mine. I provided an array_key_first() implementation awhile ago that was first = shot down because =93too many array_* functions=94, and then later = ignored because I didn=92t want to go through the RFC process just to = add a few functions. (PR here: https://github.com/php/php-src/pull/347) If people are generally interested in having an = array_key_(first|last|index) implementation, I can dust off that PR, = update it for master, and if we really need an RFC, I=92ll prepare one = for PHP 7.1. -John