RE: [PHP-DEV] array_seek function
| From: | Jared Williams | Date: | Tue, 16 Mar 2010 22:29:48 +0000 |
| Subject: | RE: [PHP-DEV] array_seek function | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-47347@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Felix De Vliegher [mailto:felix.devliegher@gmail.com]
> Sent: 16 March 2010 13:31
> To: PHP internals
> Subject: [PHP-DEV] array_seek function
>
> 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
>
Remember doing something like this in the past...
$input = array(3, 'bar', 'baz');
$iterator = new ArrayIterator($input);
$iterator->seek(2);
echo $iterator->current();
$iterator->seek(0);
echo $iterator->current();
$iterator->seek(5); // throws OutOfBoundsException
Though a specific function does make sense, imo.
Jared