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
On Thu, Apr 17, 2014 at 6:39 PM, Tjerk Meesters tjerk.meesters@gmail.comwrote:
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()
andend()
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?
Hi Levi,
On Fri, Apr 18, 2014 at 9:17 AM, Levi Morrison morrison.levi@gmail.comwrote:
On Thu, Apr 17, 2014 at 6:39 PM, Tjerk Meesters tjerk.meesters@gmail.comwrote:
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()
andend()
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?
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
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()
andend()
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
On Fri, Apr 18, 2014 at 2:39 AM, Tjerk Meesters tjerk.meesters@gmail.comwrote:
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()
andend()
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
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()
andend()
to specify the
number of elements that should be skipped (forward or backward).
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
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
Hi Peter,
On Sat, Apr 19, 2014 at 7:50 AM, Peter Cowburn petercowburn@gmail.comwrote:
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()
andend()
to specify the
number of elements that should be skipped (forward or backward).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:
- doesn't update the internal pointer if seek() failed
- returns boolean result to indicate success / failure
- 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()
andend()
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
functionCurrently, 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
--
Tjerk