Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85335 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33283 invoked from network); 20 Mar 2015 21:28:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2015 21:28:12 -0000 Authentication-Results: pb1.pair.com smtp.mail=cmbecker69@gmx.de; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=cmbecker69@gmx.de; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmx.de designates 212.227.15.19 as permitted sender) X-PHP-List-Original-Sender: cmbecker69@gmx.de X-Host-Fingerprint: 212.227.15.19 mout.gmx.net Received: from [212.227.15.19] ([212.227.15.19:59629] helo=mout.gmx.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C3/EB-64120-BE09C055 for ; Fri, 20 Mar 2015 16:28:12 -0500 Received: from [192.168.0.101] ([91.67.244.80]) by mail.gmx.com (mrgmx002) with ESMTPSA (Nemesis) id 0LwqwS-1Zap5i3s0u-016Kzo; Fri, 20 Mar 2015 22:28:07 +0100 Message-ID: <550C90EF.7080404@gmx.de> Date: Fri, 20 Mar 2015 22:28:15 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Alex Bowers , Stanislav Malyshev CC: PHP Internals References: <550B5A81.1090706@gmail.com> <550C8879.70002@gmail.com> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:4Gf9TT/ojTIGs8eH13zNFRpsGEJGt/m03nNR8/eWS4szAP/91QP 5zqghVBwCBGEkBOtexRVYDWdk9PMdiw49+bV6IkXwdKezJjLEuNOOaFRZfjH2fOrSztMkNV Wj1Hj/AevlZJivDo1WdIME+0Zgv4pYA+7q76dgZWx3DwQn1nK62jsyGt0lR26XnKR4Om+/M opozRQv5kRLRsptp6wGNQ== X-UI-Out-Filterresults: notjunk:1; Subject: Re: [PHP-DEV] RFC - Array slice syntactic sugar From: cmbecker69@gmx.de (Christoph Becker) Alex Bowers wrote: > 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. > > $results = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; > > // Old > $count = 0; > > foreach($results as $result){ > if($count++ > 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 > } // alternative old foreach(array_slice($results, 0, 9) as $result) { echo $result . "\n"; // 1 2 3 4 5 6 7 8 9 } Not so bad, in my opinion. -- Christoph M. Becker