Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85238 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 52223 invoked from network); 19 Mar 2015 20:49:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2015 20:49:19 -0000 Authentication-Results: pb1.pair.com header.from=bowersbros@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=bowersbros@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.43 as permitted sender) X-PHP-List-Original-Sender: bowersbros@gmail.com X-Host-Fingerprint: 74.125.82.43 mail-wg0-f43.google.com Received: from [74.125.82.43] ([74.125.82.43:34880] helo=mail-wg0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 13/FB-25408-E463B055 for ; Thu, 19 Mar 2015 15:49:19 -0500 Received: by wgdm6 with SMTP id m6so72923083wgd.2 for ; Thu, 19 Mar 2015 13:49:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=eUVBqOlGbMg10Pc7uP82RLr9HRRIJpGmlIwDcQeBBGs=; b=pyQF/lZkHSbakkMKhho/X7IWVBIJQCUPkpm4RrWc0zZpwB5oPiUo016QXUInXn5EAB hovrs1j8oLtMt2Zh7MHYj5J6cqPdQA2yh4yUQgeh4ahb/0U8G0/0B40a7wolfa4rYh+D VlwRh1HN3R/OF0ab/AbLz0DhU2fVNEZyKg22P+p68UtKCOMd4OSY5coPg4Lbuof20xKh bla3oc6y9UOMmF7H4OJZdge8GwnqnlikZ0KDZ57453fixEX9oUnZ8mDvXjjVhRPJJEau +Ex9Zj5vEBCcJC8FiwTkyV74ME3fUdMME3xogETClgoUhGJHRMS1fhP4Z4n/ZCvLyhZI vHiQ== MIME-Version: 1.0 X-Received: by 10.194.221.100 with SMTP id qd4mr152293059wjc.113.1426798155772; Thu, 19 Mar 2015 13:49:15 -0700 (PDT) Received: by 10.28.62.84 with HTTP; Thu, 19 Mar 2015 13:49:15 -0700 (PDT) Date: Thu, 19 Mar 2015 20:49:15 +0000 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=001a11c3aa743db9940511aa55c6 Subject: RFC - Array slice syntactic sugar From: bowersbros@gmail.com (Alex Bowers) --001a11c3aa743db9940511aa55c6 Content-Type: text/plain; charset=UTF-8 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 --001a11c3aa743db9940511aa55c6--