Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85332 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26548 invoked from network); 20 Mar 2015 21:07:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2015 21:07:35 -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.169 as permitted sender) X-PHP-List-Original-Sender: bowersbros@gmail.com X-Host-Fingerprint: 209.85.212.169 mail-wi0-f169.google.com Received: from [209.85.212.169] ([209.85.212.169:35175] helo=mail-wi0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AB/9A-64120-61C8C055 for ; Fri, 20 Mar 2015 16:07:35 -0500 Received: by wibdy8 with SMTP id dy8so1923536wib.0 for ; Fri, 20 Mar 2015 14:07:29 -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=m5TguNIfeNN8+oUH2RFP5PxPRI2tFj9Xm5wm8ANCvSs=; b=iG049Zula1jAARc67ie9kFyxMAk+AIDzI3xI/NboqM7FlY1H8QV+rCATTGSnp4vfob XeslFWHEdU2QxlWRXsEBpAoVkDmB5bQ0tMvj4px6aqjrrQwJe3E5WB9x8D5IfNnFXrGd yzq9U2l7kuvl012HdhETXv9jqGClDFulpAgoetHoNRJeJ4LH9hO9IjuvoQyc7MfmaFth x4XU6+hAY4ajOpAcsBiaYhYJmwUj4ABcAoDzhUPxhLhV0j48KVRqNlqk4J5u/mzzYXsq 2BbepFrMWBsEDt62FlQeZrOyVIGUwSkYkRhSiwxQBZvka+SKStVfp/1tRXAxkEZWbHqE D4uQ== MIME-Version: 1.0 X-Received: by 10.180.211.144 with SMTP id nc16mr28241342wic.82.1426885649866; Fri, 20 Mar 2015 14:07:29 -0700 (PDT) Received: by 10.28.62.84 with HTTP; Fri, 20 Mar 2015 14:07:29 -0700 (PDT) In-Reply-To: <550C8879.70002@gmail.com> References: <550B5A81.1090706@gmail.com> <550C8879.70002@gmail.com> Date: Fri, 20 Mar 2015 21:07:29 +0000 Message-ID: To: Stanislav Malyshev Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a11c268904ba7000511beb418 Subject: Re: [PHP-DEV] RFC - Array slice syntactic sugar From: bowersbros@gmail.com (Alex Bowers) --001a11c268904ba7000511beb418 Content-Type: text/plain; charset=UTF-8 The latest comments in this thread are talking about having a symbol before the range to show that it is by positional index. Current propositions for this are ^ and *. I'm not sure how such operation would be useful Anywhere on the front-end where a foreach() is used, and expects at most say, 10 items. But the full dataset is fetched back (to show a summary after the first 10 or whatever other reason). The old code would have required a counter, the new code does not. This would make it cleaner for the front-end developer to understand. 9) break; echo $result . "\n"; // 1 2 3 4 5 6 7 8 9 } // New foreach($results[*:9] as $result) { echo $result . "\n"; // 1 2 3 4 5 6 7 8 9 } --- $string = "abcdefghijklmnop"; // Old echo substr($string, 0, 5); // abcde // New echo $string[*:4]; // abcdef This is just a few basic examples, but should show that there is a use case for it. On 20 March 2015 at 20:52, Stanislav Malyshev wrote: > Hi! > > >> 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. > > 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. > > -- > Stas Malyshev > smalyshev@gmail.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a11c268904ba7000511beb418--