Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85241 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57491 invoked from network); 19 Mar 2015 21:06:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2015 21:06:58 -0000 Authentication-Results: pb1.pair.com smtp.mail=bowersbros@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bowersbros@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.181 as permitted sender) X-PHP-List-Original-Sender: bowersbros@gmail.com X-Host-Fingerprint: 209.85.212.181 mail-wi0-f181.google.com Received: from [209.85.212.181] ([209.85.212.181:38682] helo=mail-wi0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 21/0D-25408-17A3B055 for ; Thu, 19 Mar 2015 16:06:57 -0500 Received: by wifj2 with SMTP id j2so1371632wif.1 for ; Thu, 19 Mar 2015 14:06:54 -0700 (PDT) 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=SzQSsk2LXF2UnmVTMMKNrThBQeYIQ8cKpDP9ZYs3NCo=; b=UFF8xRpKhkozzofDRIioZGrmmT0DsTtO1droDnbcgqIAg1aap6eLxx1LaIUh1KdqxU vNrW37EB/oDKmW0sEIeFGL+xa6Z9S7uyYeNcZbGsnmsWSeu7EFZPxtNPmQaD7Cm5nBBO CgGhzXHx37KQnfVmgRRzh9Yr+i6GYSKPaH+v191b5t54a45uMKCDgz960JccF0WJeOK0 b9KwJjICLVDj2A7SplgioJrFzfHoJSqPE4lMmXonRocqjpxS5cTDeFqJEfqz9IOWgcQb 33sCxwKIb2kP7/inr4hDBYMhw1d7uQW+M2xbFxBAvFN/Hr6PS5jexNcPn+UzXbZqOs7T tzQw== MIME-Version: 1.0 X-Received: by 10.181.8.103 with SMTP id dj7mr19372502wid.75.1426799214665; Thu, 19 Mar 2015 14:06:54 -0700 (PDT) Received: by 10.28.62.84 with HTTP; Thu, 19 Mar 2015 14:06:54 -0700 (PDT) In-Reply-To: <550B39AA.2090706@garfieldtech.com> References: <550B39AA.2090706@garfieldtech.com> Date: Thu, 19 Mar 2015 21:06:54 +0000 Message-ID: To: Larry Garfield Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=001a1134676a5b26de0511aa94bb Subject: Re: [PHP-DEV] RFC - Array slice syntactic sugar From: bowersbros@gmail.com (Alex Bowers) --001a1134676a5b26de0511aa94bb Content-Type: text/plain; charset=UTF-8 I've had a quick scan of the list at https://wiki.php.net/rfc but cannot seem to find anything. I'll read more carefully through, or is there a different list elsewhere which I should look at? On 19 March 2015 at 21:03, Larry Garfield wrote: > 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a1134676a5b26de0511aa94bb--