Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:47305 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99884 invoked from network); 16 Mar 2010 13:42:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2010 13:42:23 -0000 Authentication-Results: pb1.pair.com header.from=felix.devliegher@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=felix.devliegher@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: felix.devliegher@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-wy0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:38041] helo=mail-wy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C1/9D-15129-DBA8F9B4 for ; Tue, 16 Mar 2010 08:42:21 -0500 Received: by wyb36 with SMTP id 36so1366902wyb.29 for ; Tue, 16 Mar 2010 06:42:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:subject:mime-version :content-type:from:in-reply-to:date:cc:message-id:references:to :x-mailer; bh=faxNgAljkO09SCE0jA4bCRU3zN7NhBmpbI/Ie4kTBps=; b=Y2NeD/oCnsAEmvuNGJbw6uGiFj7ptDlLTNotZMuqFZA/jMWXhJNp3CgC2zoj0LZOob kWlEuc1Huh7hXse9qA//SyKmgpL7loR2teUYH9flPQVukHyOSTlZjIDrHU1RF0wQBeQ2 bf+z4QpuwvHMet5FHje2lS4RKxHB8ITxyFahQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :message-id:references:to:x-mailer; b=k4iwQ8RH4cdAnFkXwlnVRfQF0LYva9MFFkwbDf1XO8Jtrl7nv/xn2KSbDkfkK2SZsF cubEvQmPs99+KBHLvJgiH0DR8bZXS/0gZc7+o2h0MnO2NEbS/N24FoLygIWbtTgJNBd5 FYXB8mtldGPkbEu9Xoaf5zK03gU55ue6WK2qY= Received: by 10.216.87.204 with SMTP id y54mr391066wee.193.1268746937423; Tue, 16 Mar 2010 06:42:17 -0700 (PDT) Received: from [10.0.1.102] (cable-175-208.zeelandnet.nl [82.176.175.208]) by mx.google.com with ESMTPS id n12sm16132619gve.15.2010.03.16.06.42.14 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 16 Mar 2010 06:42:16 -0700 (PDT) Mime-Version: 1.0 (Apple Message framework v1077) Content-Type: multipart/mixed; boundary=Apple-Mail-25-529440970 In-Reply-To: Date: Tue, 16 Mar 2010 14:42:13 +0100 Cc: PHP internals Message-ID: <1E2D6F10-F23D-4F08-BFFD-13D5E6CF6D51@gmail.com> References: <6E76B52E-7543-4F09-A948-A7910513B548@gmail.com> To: Pierre Joye X-Mailer: Apple Mail (2.1077) Subject: Re: [PHP-DEV] array_seek function From: felix.devliegher@gmail.com (Felix De Vliegher) --Apple-Mail-25-529440970 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii 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 --Apple-Mail-25-529440970 Content-Disposition: attachment; filename=array_seek.txt Content-Type: text/plain; name="array_seek.txt" Content-Transfer-Encoding: quoted-printable Index: ext/standard/array.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- ext/standard/array.c (revision 296276) +++ ext/standard/array.c (working copy) @@ -4507,6 +4507,41 @@ } /* }}} */ =20 +/* {{{ 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 =3D 0; + long pos; + zval *array, **entry; + HashPosition hpos; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "al", = &array, &pos) =3D=3D FAILURE) { + return; + } + + /* Get number of entries in the array */ + num_in =3D 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 <=3D pos && = zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **)&entry, &hpos) = =3D=3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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) =20 /* aliases from array.c */ PHP_FALIAS(pos, current, = arginfo_current) Index: ext/standard/php_array.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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); =20 PHPAPI HashTable* php_splice(HashTable *, int, int, zval ***, int, = HashTable **); PHPAPI int php_array_merge(HashTable *dest, HashTable *src, int = recursive TSRMLS_DC); --Apple-Mail-25-529440970 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On 16-mrt-2010, at 14:34, Pierre Joye wrote: > hi Felix, >=20 > 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 :)? >=20 > Cheers, >=20 > Cheers, >=20 > On Tue, Mar 16, 2010 at 2:30 PM, Felix De Vliegher > wrote: >> Hi all >>=20 >> 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 =3D 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) >>=20 >> Basically you can use it like this: >> $input =3D 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 >>=20 >> 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. >>=20 >> Attached is a patch for the function against the 5.3 branch. If = approved, I could add it to svn + testcases + docs. Feedback please :-) >>=20 >>=20 >> Kind regards, >> Felix >>=20 >>=20 >>=20 >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >>=20 >=20 >=20 >=20 > --=20 > Pierre >=20 > @pierrejoye | http://blog.thepimp.net | http://www.libgd.org --Apple-Mail-25-529440970--