Re: [pr-645] Add offset to reset() and end()
| From: | Tjerk Meesters | Date: | Fri, 18 Apr 2014 01:48:36 +0000 |
| Subject: | Re: [pr-645] Add offset to reset() and end() | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-73737@lists.php.net to get a copy of this message | ||
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:
>
>> 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?
>
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