Galbar/JsonPath-PHP

ArraySlice

infisamk opened this issue · 3 comments

In sliceIndices condition is:

while ($step < 0 ? ($i > $stop) : ($i < $stop))

But why is not:

while ($step < 0 ? ($i >= $stop) : ($i <= $stop))

If I get $.store.book[1:3]. I get only second and third items, but not second, third and fourth items.

It is not a bug, it is by design.

This allows for expressions such as "get the last five elements":
array[array.length - 5 : array.length]

All array slice operators I've seen out there work in an non inclusive manner for the ending index.

Thank you for answer!

Array is stared from 0 index. Then:

array[array.length - 5 : array.length - 1]

Since the last element of the array will have an index array.length - 1, but not array.length, as you wrote.

And if you insist, then you description for example is not right:

$.store.book[1:3] | From the second book to the fourth.

The right description for this example is must be:

$.store.book[1:3] | From the second book to the third.

Thank you for pointing out the mistake in the documentation. I'll be updating it