Re: RFC - Array slice syntactic sugar
| From: | Stanislav Malyshev | Date: | Fri, 20 Mar 2015 20:52:09 +0000 |
| Subject: | Re: RFC - Array slice syntactic sugar | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-85329@lists.php.net to get a copy of this message | ||
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