# Support negative indexes for arrays and strings

- Thread: https://externals.io/message/62735
- Original archive: https://news-web.php.net/php.internals/62735
- Started by slevy1@pipeline.com on 2012-09-03T12:41:18+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #62735 — slevy1@pipeline.com — 2012-09-03T12:41:18+00:00

URL: https://externals.io/message/62735#62735

>as I understood, a lot of people seem to have problems with this proposal, since arrays and strings could (and maybe >should) behave the same.

The terminology "negative indexing" seems to imply that the feature should work with arrays. To restrict it to just strings might involve creating a term one would only associate with strings.


>The main problem arises from the ambiguity for $array[-1] for arrays.
>But this is easily solvable: just introduce a slice operator.
>
>$array[:-1] and the ambiguity is gone.

Thanks for that suggestion!  On the one hand, negative indexing alone is simpler and less prone to typos, but it makes sense that using the slice operator would clarify the code. 

SL

---

## #62762 — Stas Malyshev — 2012-09-03T19:59:07+00:00

URL: https://externals.io/message/62735#62762 · In reply to: #62735

Hi!

> The terminology "negative indexing" seems to imply that the feature
> should work with arrays. To restrict it to just strings might involve
> creating a term one would only associate with strings.

I do not see any way to change behavior of $array[-1] that would make
sense. So either only strings get it or nobody gets it.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #62793 — Tjerk Meesters — 2012-09-04T13:43:24+00:00

URL: https://externals.io/message/62735#62793 · In reply to: #62762

> Hi!
> 
> > […]
> 
> I do not see any way to change behavior of $array[-1] that would make
> sense. So either only strings get it or nobody gets it.

I suppose one could still extend ArrayAccess and have their own negative indices support for arrays :)

> 
> -- 
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
