# [pr-645] Add offset to reset() and end()

- Thread: https://externals.io/message/73735
- Original archive: https://news-web.php.net/php.internals/73735
- Started by Tjerk Meesters on 2014-04-18T00:39:41+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #73735 — Tjerk Meesters — 2014-04-18T00:39:41+00:00

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

Hi,

I've added a PR that satisfies #31375.

https://github.com/php/php-src/pull/645

It adds a second optional argument to `reset()` and `end()` to specify the
number of elements that should be skipped (forward or backward).

Currently, negative offsets are ignored. Should we raise a notice for such
cases? Something like:

"Negative offsets are not supported."

Any feedback is welcome. Thanks!


-- 
--
Tjerk

---

## #73736 — Levi Morrison — 2014-04-18T01:17:11+00:00

URL: https://externals.io/message/73735#73736 · In reply to: #73735

On Thu, Apr 17, 2014 at 6:39 PM, Tjerk Meesters <tjerk.meesters@gmail.com>wrote:

> Hi,
>
> I've added a PR that satisfies #31375.
>
> https://github.com/php/php-src/pull/645
>
> It adds a second optional argument to `reset()` and `end()` to specify the
> number of elements that should be skipped (forward or backward).
>
> Currently, negative offsets are ignored. Should we raise a notice for such
> cases? Something like:
>
> "Negative offsets are not supported."
>
> Any feedback is welcome. Thanks!
>

Do you have a specific use-case for these changes?

---

## #73737 — Tjerk Meesters — 2014-04-18T01:48:36+00:00

URL: https://externals.io/message/73735#73737 · In reply to: #73736

Hi Levi,


On Fri, Apr 18, 2014 at 9:17 AM, Levi Morrison <morrison.levi@gmail.com>wrote:

> On Thu, Apr 17, 2014 at 6:39 PM, Tjerk Meesters <tjerk.meesters@gmail.com>wrote:
>
> > […]
>
> Do you have a specific use-case for these changes?
>

Surely you must have seen the feature request on bugs.php.net ;-)

One example could be, traverse array but skip the first N elements:

    $array = [1, 2, 3];
    for (reset($array, 1); $current = each($array); ) {
        var_dump($current['value']);
    }
    // output: int(2) int(3)

Or, get second last element:

    $second_last = end($array, 1); // int(2)


-- 
--
Tjerk

---

## #73739 — Stas Malyshev — 2014-04-18T05:50:00+00:00

URL: https://externals.io/message/73735#73739 · In reply to: #73735

Hi!

> I've added a PR that satisfies #31375.
> 
> https://github.com/php/php-src/pull/645
> 
> It adds a second optional argument to `reset()` and `end()` to specify the
> number of elements that should be skipped (forward or backward).

I'm not sure `end()` part makes sense for most iterators (I know reset and
end doesn't currently work on Iterator, but at least for reset there's
no reason why it should not). For reset, it's pretty trivial to
implement it in user code.

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

---

## #73741 — Nikita Popov — 2014-04-18T08:58:54+00:00

URL: https://externals.io/message/73735#73741 · In reply to: #73735

On Fri, Apr 18, 2014 at 2:39 AM, Tjerk Meesters <tjerk.meesters@gmail.com>wrote:

> Hi,
>
> I've added a PR that satisfies #31375.
>
> https://github.com/php/php-src/pull/645
>
> It adds a second optional argument to `reset()` and `end()` to specify the
> number of elements that should be skipped (forward or backward).
>
> Currently, negative offsets are ignored. Should we raise a notice for such
> cases? Something like:
>
> "Negative offsets are not supported."
>
> Any feedback is welcome. Thanks!
>

The `reset()` part seems to already be adequately covered by LimitIterator.
Personally I'd prefer not to add any new functionality to reset/current/...
because you oughtn't be using those functions anyway. Trying to do manual
array iteration with them is both somewhat unsafe (think nested iteration)
and makes for really ugly code.

// skip 10 elements of $it. Can be an ArrayIterator, of course
foreach (new LimitIterator($it, 10) as $k => $v) { ... }

Nikita

---

## #73742 — Peter Cowburn — 2014-04-18T23:50:58+00:00

URL: https://externals.io/message/73735#73742 · In reply to: #73741

> On Fri, Apr 18, 2014 at 2:39 AM, Tjerk Meesters <tjerk.meesters@gmail.com
> > […]
>
> > […]
>

The last time that I can remember discussion of something like array_seek()
[1]–which is what this seems like it actually wants to be–the reception was
fairly positive, but the idea just fizzled out.  I’d recommend looking back
over the archives for other similar discussions.

I would be -1 on changing `reset()` and `end()` to make them be able to not
reset the array pointer, or not move to the end, respectively; especially
where a neater (IMO) alternative has already been discussed.

 [1] http://markmail.org/thread/sczgritz5jcccnwh [PHP-DEV] array_seek
function



> > […]
> such
> > […]
>
> The `reset()` part seems to already be adequately covered by LimitIterator.
> Personally I'd prefer not to add any new functionality to reset/current/...
> because you oughtn't be using those functions anyway. Trying to do manual
> array iteration with them is both somewhat unsafe (think nested iteration)
> and makes for really ugly code.
>
> // skip 10 elements of $it. Can be an ArrayIterator, of course
> foreach (new LimitIterator($it, 10) as $k => $v) { ... }
>
> Nikita

---

## #73770 — Tjerk Meesters — 2014-04-24T03:28:01+00:00

URL: https://externals.io/message/73735#73770 · In reply to: #73742

Hi Peter,


On Sat, Apr 19, 2014 at 7:50 AM, Peter Cowburn <petercowburn@gmail.com>wrote:

>
>
>
> 
>
> > […]
>
> The last time that I can remember discussion of something like
> array_seek() [1]–which is what this seems like it actually wants to be–the
> reception was fairly positive, but the idea just fizzled out.  I’d
> recommend looking back over the archives for other similar discussions.
>

Thanks for digging up the earlier discussion; taking also into account the
other feedback I have received, I have updated the PR:

https://github.com/php/php-src/pull/645

It takes some cues from the earlier `array_seek()` proposal but:
1. doesn't update the internal pointer if seek() failed
2. returns boolean result to indicate success / failure
3. is not called `array_seek()` ;-)

I would be perfectly okay if this gets shut down as well, but at least I've
tried :)

Example of "iterate over last 50 array entries":

if (array_seek($array, -50, SEEK_END)) {
    while ($data = each($array)) {
        ...
    }
}


> I would be -1 on changing `reset()` and `end()` to make them be able to not
> reset the array pointer, or not move to the end, respectively; especially
> where a neater (IMO) alternative has already been discussed.
>
>  [1] http://markmail.org/thread/sczgritz5jcccnwh [PHP-DEV] array_seek
> function
>
>
>
> > […]
>
>


-- 
--
Tjerk
