Re: Support negative indexes for arrays and strings
| From: | Tjerk Meesters | Date: | Sat, 01 Sep 2012 03:59:53 +0000 |
| Subject: | Re: Support negative indexes for arrays and strings | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62617@lists.php.net to get a copy of this message | ||
On 1 Sep, 2012, at 1:11 AM, Rasmus Schultz <rasmus@mindplay.dk> wrote:
> Yes, typo! sorry.
>
> $array = array(1001, 1002, 1003, 1004);
> $number = $array[-1]; // => 1004
> $array[-1] = 1005;
> $number = $array[-1]; // => ????
>
> Looking at the resulting code, I would like to point out also that
> it's extremely misleading... because $array[-1] references two
> completely different elements depending on whether you're reading or
> writing the value... unless $array[-1] = 1005 would actually overwrite
> the last element in the array - in which case it gets even more
> dangerous, as well as breaking backwards compatibility...
That might actually be something I could use :) But the fun for me begins here:
$numbers = array();
$numbers[-1] = 5;
$numbers[] = 6;
What would have happened to the keys? Normally [] is equivalent to [count($numbers)].
Perhaps this is why JS treats negative indices as properties instead :)
>
>
> On Fri, Aug 31, 2012 at 9:24 AM, Ferenc Kovacs <tyra3l@gmail.com> wrote:
>>
>>
>> On Fri, Aug 31, 2012 at 3:14 PM, Rasmus Schultz <rasmus@mindplay.dk> wrote:
>>>
>>> Having thought about this for a while, I think this is a bad idea - here's
>>> why:
>>>
>>> $array = array(1001, 1002, 1003, 1004);
>>>
>>> $number = $array[-1]; // => 1004
>>>
>>> $number[-1] = 1005;
>>>
>>> $number = $array[-1]; // => ????
>>>
>>> Obviously, the last statement must return 1005, since otherwise that
>>> value would be inaccessible.
>>>
>>
>> maybe you wanted to write
>> $array[-1] = 1005;
>> instead of
>> $number[-1] = 1005;
>> ?
>>
>> otherwise I can't see the problem that you are mentioning.
>> could you clarify?
>>
>> --
>> Ferenc Kovács
>> @Tyr43l - http://tyrael.hu
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>