Re: array_seek function
| From: | Felix De Vliegher | Date: | Wed, 17 Mar 2010 16:13:24 +0000 |
| Subject: | Re: array_seek function | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-47366@lists.php.net to get a copy of this message | ||
On 17-mrt-2010, at 17:00, Patrick ALLAERT wrote:
> 2010/3/16 Felix De Vliegher <felix.devliegher@gmail.com>:
>> Hi all
>>
>> I recently needed seek functionality in arrays, and couldn't find it in the regular
>> set of array functions, so I wrote a function for it. (Seek = getting an array value based on the
>> position (or offset, if you want to call it like that), and not the key of the item)
>>
>> Basically you can use it like this:
>> $input = array(3, 'bar', 'baz');
>> echo array_seek($input, 2); // returns 'baz'
>> echo array_seek($input, 0); // returns 3
>> echo array_seek($input, 5); // returns NULL, emits an out of range warning
>>
>> I was wondering if it's useful to add this to the family of array functions. I know
>> there is a somewhat similar thing in SPL (ArrayIterator::seek), but that doesn't work exactly
>> like what I was aiming for.
>>
>> Attached is a patch for the function against the 5.3 branch. If approved, I could add it to
>> svn + testcases + docs. Feedback please :-)
>>
>>
>> Kind regards,
>> Felix
>
> Hi Felix,
>
> What about adding a new int parameter to PHP array function: next()
> and prev() instead of adding a new one?
Hi Patrick
That's of course possible, but that would only allow seeking to a relative position to where
the array pointer is at that moment. The functionality also gets a bit ambiguous by adding an
optional parameter. You're not going to the next or prev position of the array anymore.
And I think functions like next, prev, current etc should be kept easy to use. If you want seek-like
functionality, use the designated array function for that (if this one should make it into php, that
is).
Cheers,
Felix