Re: RFC - Array slice syntactic sugar

From: Date: Fri, 20 Mar 2015 21:04:50 +0000
Subject: Re: RFC - Array slice syntactic sugar
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-85330@lists.php.net to get a copy of this message
On Mar 20, 2015, at 16:52, Stanislav Malyshev <smalyshev@gmail.com> 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. My interest in this proposal is mostly because it can potentially provide a convenient way to get the first element in an array, which is a not-infrequent need of mine. I provided an array_key_first() implementation awhile ago that was first shot down because “too many array_* functions”, and then later ignored because I didn’t want to go through the RFC process just to add a few functions. (PR here: https://github.com/php/php-src/pull/347) If people are generally interested in having an array_key_(first|last|index) implementation, I can dust off that PR, update it for master, and if we really need an RFC, I’ll prepare one for PHP 7.1. -John

« previous php.internals (#85330) next »