Hi Pierre
Right now, it returns the value of a given position. In that case,
array_get_pos might be a better name. Oh, and I attached the patch with .txt
extension :)
Greetings,
Felix
Index: ext/standard/array.c
===================================================================
--- ext/standard/array.c (revision 296276)
+++ ext/standard/array.c (working copy)
@@ -4507,6 +4507,41 @@
}
/* }}} */
+/* {{{ proto array array_seek(array input, int position)
+ Finds the array value which matches the position of that element */
+PHP_FUNCTION(array_seek)
+{
+ int num_in;
+ int currentpos = 0;
+ long pos;
+ zval *array, **entry;
+ HashPosition hpos;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "al", &array,
&pos) == FAILURE) {
+ return;
+ }
+
+ /* Get number of entries in the array */
+ num_in = zend_hash_num_elements(Z_ARRVAL_P(array));
+
+ /* Check if we have a valid position. */
+ if (pos > num_in - 1 || pos < 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Seek position %ld is out of
range", pos);
+ return;
+ }
+
+ /* Loop over the input array untill we are at the right position */
+ zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &hpos);
+ while (currentpos <= pos && zend_hash_get_current_data_ex(Z_ARRVAL_P(array),
(void **)&entry, &hpos) == SUCCESS) {
+ currentpos++;
+ zend_hash_move_forward_ex(Z_ARRVAL_P(array), &hpos);
+ }
+
+ /* Return the matching element */
+ RETURN_ZVAL(*entry, 1, 0);
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4
Index: ext/standard/basic_functions.c
===================================================================
--- ext/standard/basic_functions.c (revision 296276)
+++ ext/standard/basic_functions.c (working copy)
@@ -609,6 +609,11 @@
ZEND_ARG_INFO(0, keys) /* ARRAY_INFO(0, keys, 0) */
ZEND_ARG_INFO(0, values) /* ARRAY_INFO(0, values, 0) */
ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO(arginfo_array_seek, 0)
+ ZEND_ARG_INFO(0, input) /* ARRAY_INFO(0, input, 0) */
+ ZEND_ARG_INFO(0, position) /* ARRAY_INFO(0, position, 0) */
+ZEND_END_ARG_INFO()
/* }}} */
/* {{{ basic_functions.c */
ZEND_BEGIN_ARG_INFO(arginfo_get_magic_quotes_gpc, 0)
@@ -3320,6 +3325,7 @@
PHP_FE(array_chunk,
arginfo_array_chunk)
PHP_FE(array_combine,
arginfo_array_combine)
PHP_FE(array_key_exists,
arginfo_array_key_exists)
+ PHP_FE(array_seek, arginfo_array_seek)
/* aliases from array.c */
PHP_FALIAS(pos, current,
arginfo_current)
Index: ext/standard/php_array.h
===================================================================
--- ext/standard/php_array.h (revision 296276)
+++ ext/standard/php_array.h (working copy)
@@ -101,6 +101,7 @@
PHP_FUNCTION(array_key_exists);
PHP_FUNCTION(array_chunk);
PHP_FUNCTION(array_combine);
+PHP_FUNCTION(array_seek);
PHPAPI HashTable* php_splice(HashTable *, int, int, zval ***, int, HashTable
**);
PHPAPI int php_array_merge(HashTable *dest, HashTable *src, int recursive
TSRMLS_DC);
On 16-mrt-2010, at 14:34, Pierre Joye wrote:
> hi Felix,
>
> Not sure about the usefulness of this function but the name is
> misleading (pls reattach the patch as .txt while being at it :). Does
> it set the position (_seek) or does it return the value of a given
> position (_get_pos)? or both (no idea :)?
>
> Cheers,
>
> Cheers,
>
> On Tue, Mar 16, 2010 at 2:30 PM, Felix De Vliegher
> <[email protected]> wrote:
>> 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
>>
>>
>>
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
>
>
> --
> Pierre
>
> @pierrejoye | http://blog.thepimp.net | http://www.libgd.org
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php