Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85346 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68142 invoked from network); 20 Mar 2015 22:25:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2015 22:25:43 -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.177 as permitted sender) X-PHP-List-Original-Sender: bowersbros@gmail.com X-Host-Fingerprint: 209.85.212.177 mail-wi0-f177.google.com Received: from [209.85.212.177] ([209.85.212.177:35199] helo=mail-wi0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CB/93-64120-66E9C055 for ; Fri, 20 Mar 2015 17:25:43 -0500 Received: by wibdy8 with SMTP id dy8so3109419wib.0 for ; Fri, 20 Mar 2015 15:25:40 -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=87O/EFuD66pQgVxOQob0Ua5GoSO8LGv/wCRmI4eIGwI=; b=HFQSYHIkvc84G2AYfDvWTsYVCpBhSDK5CRKEIZ7VW8rj+ko3GGXrD76s+Ntc6XPmR7 0gjLJM/ewtf4qfo+j4xQZMbqeWG9PaZxJ9Rpzk1DGSZrTwo3Ew6sLfuD9SCCsqIH2IMy VgLS1xksi0Ka3LPnI/BWYNKe3ohnTLn/OwojxLaTg/59zMJMi7cgK1g5jSH4TJhnJ2y+ hR2W/0/g8WVxRPcB2N8qPZL6GbnxPPMbIY+WmDLrBedptPLEO3L+xxr84Ut1W2Z8jvWS Qklii319gzOzgQsxM8QE2jvgGR4p1Qx8VaflYj0UNyxxig/eYorIvKEynocaCxsMgi48 MENw== MIME-Version: 1.0 X-Received: by 10.180.23.99 with SMTP id l3mr22746wif.36.1426890339912; Fri, 20 Mar 2015 15:25:39 -0700 (PDT) Received: by 10.28.62.84 with HTTP; Fri, 20 Mar 2015 15:25:39 -0700 (PDT) In-Reply-To: <550C9B39.5080801@gmail.com> References: <550B5A81.1090706@gmail.com> <550C8879.70002@gmail.com> <550C90EF.7080404@gmx.de> <550C9B39.5080801@gmail.com> Date: Fri, 20 Mar 2015 22:25:39 +0000 Message-ID: To: Stanislav Malyshev Cc: Christoph Becker , PHP Internals Content-Type: multipart/alternative; boundary=001a11342910d8136a0511bfcbe6 Subject: Re: [PHP-DEV] RFC - Array slice syntactic sugar From: bowersbros@gmail.com (Alex Bowers) --001a11342910d8136a0511bfcbe6 Content-Type: text/plain; charset=UTF-8 On 20 March 2015 at 22:12, Stanislav Malyshev wrote: > You're not using the keys in foreach, so why you need to preserve them? This was merely an example of the features equal part in current code, not a real life use case. Using the new syntax will keep the keys preserved, therefore any example showing how to do the same *must* do the same, which preserving keys does. This may be not so easy to implement - imagine passing $array[*1:4] by > reference. This would be the same as doing $array[array_keys($old_array)[1]] = $new_array[0]; $array[array_keys($old_array)[2]] = $new_array[1]; $array[array_keys($old_array)[3]] = $new_array[2]; $array[array_keys($old_array)[4]] = $new_array[3]; The new syntax helps clean that up, by doing: $array[*1:4] = [1,2,3,4]; There is nothing to pass by reference that is different, the array is still $array, it is just having the values replaced in bulk by index (not by key) This sort of code would be needed: 1, 1 => 2, 5 => 3, 7 => 4, 4 => 5, 2 => 6, 14 => 7, 55 => 8]; $new_array_values = ['foo', 'bar', 'baz']; // To replace the third, fourth and fifth element. $old_array[array_keys($old_array)[3]] = $new_array_values[0]; $old_array[array_keys($old_array)[4]] = $new_array_values[1]; $old_array[array_keys($old_array)[5]] = $new_array_values[2]; var_dump($old_array); Instead of, what is in my opinion, much cleaner: 1, 1 => 2, 5 => 3, 7 => 4, 4 => 5, 2 => 6, 14 => 7, 55 => 8]; $new_array_values = ['foo', 'bar', 'baz']; $old_array[*3:5] = $new_array_values; var_dump($old_array); There is the array_splice method that can do the same thing, however this to me is less obvious as to what is happening: 1, 1 => 2, 5 => 3, 7 => 4, 4 => 5, 2 => 6, 14 => 7, 55 => 8]; $new_array_values = ['foo', 'bar', 'baz']; // To replace the third, fourth and fifth element. array_splice($old_array, 3, 3, $new_array_values); var_dump($old_array); --001a11342910d8136a0511bfcbe6--