Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85240 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55965 invoked from network); 19 Mar 2015 21:03:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2015 21:03:42 -0000 Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.27 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.27 out3-smtp.messagingengine.com Received: from [66.111.4.27] ([66.111.4.27:58069] helo=out3-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1A/AC-25408-DA93B055 for ; Thu, 19 Mar 2015 16:03:42 -0500 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 436E520B5E for ; Thu, 19 Mar 2015 17:03:37 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Thu, 19 Mar 2015 17:03:39 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=x-sasl-enc:message-id:date:from :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=dsdT2wN6cfoZ9rJJOl0qkP sgOKk=; b=mmmi5Ee2xphWirlZGgfG6hNzckHSSd5KfdwIker8DLaTo1ia30SyXd GJpezPp41AJS/B7Hc+Wr2JVlAPfRM8M9u0H5odlJwKmeVuOE+sMS0N3+q0LdWhCc 8f9s298STH4AEBIfQsIjnaB0GAQe74wPYtOqmirypQFvUZDMzSTLE= X-Sasl-enc: bXcvMCE1zt03A0C58G7dHklJ7QN8ksNGdMbDD2gSIs98 1426799019 Received: from Palantirs-MacBook-Pro-7.local (unknown [63.250.249.138]) by mail.messagingengine.com (Postfix) with ESMTPA id 2980AC00024 for ; Thu, 19 Mar 2015 17:03:39 -0400 (EDT) Message-ID: <550B39AA.2090706@garfieldtech.com> Date: Thu, 19 Mar 2015 16:03:38 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] RFC - Array slice syntactic sugar From: larry@garfieldtech.com (Larry Garfield) On 3/19/15 3:49 PM, Alex Bowers wrote: > This email is just to gauge the response for some syntactic sugar to be > added to PHP in regard to slicing an array. > > My proposal is something similar to Pythons slice, in PHP this would look > like: > > $slided = $array[1:4] > > 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. Any multi-dimensions are > also respected and returned within the array. > > This is the same as using the array_slice method with the PRESERVE KEYS > fourth parameter passed through as true. > > The result for a string is the string from the two positions indicated. > This is the same as using substr(). > > The benefits for this as I see it is: > > 1) No function overhead > 2) More readable (opinionated) > 3) Consistent with how we can select items from an array currently (using > index). > > If the array is not long enough (for example, index 4 doesn't exist), then > a NOTICE is thrown, and the values returned are as much as possible; this > would be the same as calling $array[1:] which will get the items in > position 1 through to the end. > > If the variable used contains a string, then this will get the values from > the string at those positions. The same way that $string[1] will get the > second character, $string[2:5] will get the third through to the sixth > character. This differs from array_slice which would throw a warning and > return null. > > If the variable isn't either a string or an array (or convertible to > either); then a warning is thrown and null is returned, consistent with > current use ($int[0] will return null) > > Arrays with associated keys cannot be selected by using the keys they have, > but instead should be selected by the position those keys hold. > > For example, this is invalid: > > $array['string':'end']. This should throw a fatal error. > > The valid options are: > > $array[from:to] - This gets the values from position 'from' to 'to' > inclusive > $array[from:] - This gets the values from the position 'from' to the end. > $array[:to] - This gets the values from the start to the position 'to'. > > $array[:] will get all the items in the array ($same as doing $array) > > A side addition, that may be considered is adding [-1] to get the last item > in the array. But that is not a main part of this RFC. > > Thanks Variations of this proposal have been discussed many times. I don't recall what the pushback was but it's worth your time to check the archives to see what the objections were and if you can address them, and/or if the new engine in PHP 7 addresses them. (I suspect it has/will ameliorate a lot of implementation-level issues with old proposals.) Personally I'd be in favor of such a syntax but I don't recall the objections in the past beyond "meh, sugar". --Larry Garfield