Re: [PHP-DEV] array_seek function

2010-04-15 Thread Derick Rethans
On Tue, 13 Apr 2010, Felix De Vliegher wrote:

  Update: http://phpbenelux.eu/array_seek-return.patch.txt
  I've kept the fseek()-style return values (0 when fine, -1 when seek 
  fails)
  
  Any reason why you picked that over the (IMO more logical) true/false 
  approach?
  
  No, it makes more sense to use the boolean return values, I was just 
  using your fseek() analogy. Although I still find it useful to return 
  the seeked value, and false when seek fails (basically how next(), 
  reset() and friends behave).
  
  Has this been added to trunk now? Or not yet?
 
 No, still have it lying around. Can I commit this?

Dunno... did you go with true/false in the end, or returning the value? 
I just realize that in the latter case you wouldn't be able to see 
whether the seek actually worked, because every value could be a 
correct value. I'd prefer the true/false thing still because of that.

Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-04-15 Thread Peter Cowburn
2010/4/15 Derick Rethans der...@php.net:
 On Tue, 13 Apr 2010, Felix De Vliegher wrote:

  Update: http://phpbenelux.eu/array_seek-return.patch.txt
  I've kept the fseek()-style return values (0 when fine, -1 when seek 
  fails)
 
  Any reason why you picked that over the (IMO more logical) true/false
  approach?
 
  No, it makes more sense to use the boolean return values, I was just
  using your fseek() analogy. Although I still find it useful to return
  the seeked value, and false when seek fails (basically how next(),
  reset() and friends behave).
 
  Has this been added to trunk now? Or not yet?

 No, still have it lying around. Can I commit this?

 Dunno... did you go with true/false in the end, or returning the value?
 I just realize that in the latter case you wouldn't be able to see
 whether the seek actually worked, because every value could be a
 correct value. I'd prefer the true/false thing still because of that.

I realise that I'm late to the game here but do have a couple of thoughts.
* The function name: this will go hand-in-hand with functions like
current(), next(), prev(), key(), reset() rather than the other
array_* functions... wouldn't seek be more appropriate given its
siblings?
* The return value: the purpose of the function is to seek, not to get
what is at the seeked-to position (right?) so true/false (+ warning?)
makes most sense to me.


 Derick

 --
 http://derickrethans.nl | http://xdebug.org
 Like Xdebug? Consider a donation: http://xdebug.org/donate.php
 twitter: @derickr and @xdebug

 --
 PHP Internals - PHP Runtime Development Mailing List
 To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-04-13 Thread Felix De Vliegher
On 12-apr-2010, at 10:20, Derick Rethans wrote:

 On Mon, 22 Mar 2010, Felix De Vliegher wrote:
 
 On 17-mrt-2010, at 19:09, Derick Rethans wrote:
 
 On Wed, 17 Mar 2010, Felix De Vliegher wrote:
 
 On 17-mrt-2010, at 17:52, Derick Rethans wrote:
 
 On Wed, 17 Mar 2010, Felix De Vliegher wrote:
 
 On 17-mrt-2010, at 17:27, Frederic Hardy wrote:
 
 Why not use arrayIterator::seek() ?
 
 Because the functionality isn't exactly the same. 
 ArrayIterator::seek() only sets the array pointer, array_seek would 
 also return the value + have fseek()-like functionality with the 
 SEEK_* consts and optional negative offsets.
 
 To be honest, I'd rather have the proposed array_seek() return a status 
 whether the seek worked or not. Notices are uncool and you can already 
 retrieve data/key with key() and current(). 
 
 
 Update: http://phpbenelux.eu/array_seek-return.patch.txt
 I've kept the fseek()-style return values (0 when fine, -1 when seek fails)
 
 Any reason why you picked that over the (IMO more logical) true/false 
 approach?
 
 No, it makes more sense to use the boolean return values, I was just 
 using your fseek() analogy. Although I still find it useful to return 
 the seeked value, and false when seek fails (basically how next(), 
 reset() and friends behave).
 
 Has this been added to trunk now? Or not yet?

No, still have it lying around. Can I commit this?

Cheers,
Felix
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-04-12 Thread Derick Rethans
On Mon, 22 Mar 2010, Felix De Vliegher wrote:

 On 17-mrt-2010, at 19:09, Derick Rethans wrote:
 
  On Wed, 17 Mar 2010, Felix De Vliegher wrote:
  
  On 17-mrt-2010, at 17:52, Derick Rethans wrote:
  
  On Wed, 17 Mar 2010, Felix De Vliegher wrote:
  
  On 17-mrt-2010, at 17:27, Frederic Hardy wrote:
  
  Why not use arrayIterator::seek() ?
  
  Because the functionality isn't exactly the same. 
  ArrayIterator::seek() only sets the array pointer, array_seek would 
  also return the value + have fseek()-like functionality with the 
  SEEK_* consts and optional negative offsets.
  
  To be honest, I'd rather have the proposed array_seek() return a status 
  whether the seek worked or not. Notices are uncool and you can already 
  retrieve data/key with key() and current(). 
  
  
  Update: http://phpbenelux.eu/array_seek-return.patch.txt
  I've kept the fseek()-style return values (0 when fine, -1 when seek fails)
  
  Any reason why you picked that over the (IMO more logical) true/false 
  approach?
 
 No, it makes more sense to use the boolean return values, I was just 
 using your fseek() analogy. Although I still find it useful to return 
 the seeked value, and false when seek fails (basically how next(), 
 reset() and friends behave).

Has this been added to trunk now? Or not yet?

Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-22 Thread Felix De Vliegher
On 17-mrt-2010, at 19:09, Derick Rethans wrote:

 On Wed, 17 Mar 2010, Felix De Vliegher wrote:
 
 On 17-mrt-2010, at 17:52, Derick Rethans wrote:
 
 On Wed, 17 Mar 2010, Felix De Vliegher wrote:
 
 On 17-mrt-2010, at 17:27, Frederic Hardy wrote:
 
 Why not use arrayIterator::seek() ?
 
 Because the functionality isn't exactly the same. 
 ArrayIterator::seek() only sets the array pointer, array_seek would 
 also return the value + have fseek()-like functionality with the 
 SEEK_* consts and optional negative offsets.
 
 To be honest, I'd rather have the proposed array_seek() return a status 
 whether the seek worked or not. Notices are uncool and you can already 
 retrieve data/key with key() and current(). 
 
 
 Update: http://phpbenelux.eu/array_seek-return.patch.txt
 I've kept the fseek()-style return values (0 when fine, -1 when seek fails)
 
 Any reason why you picked that over the (IMO more logical) true/false 
 approach?

No, it makes more sense to use the boolean return values, I was just using your 
fseek() analogy. Although I still find it useful to return the seeked value, 
and false when seek fails (basically how next(), reset() and friends behave).

Johannes: If we decide on the details, if this something I could commit in 5.3 
or do you rather want me to wait until 5.4?

Cheers,
Felix
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Daniel Convissor
Hi:

 $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

Why waste time calling a function when this can be done right now by 
acting on the array itself?

$input = array(3, 'bar', 'baz');
echo $input[2]; // returns 'baz'
echo $input[0]; // returns 3
echo $input[5]; // returns NULL, Notice: Undefined offset: 5

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
data intensive web and database programming
http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Pierre Joye
On Wed, Mar 17, 2010 at 4:02 PM, Daniel Convissor
dani...@analysisandsolutions.com wrote:
 Hi:

 $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

 Why waste time calling a function when this can be done right now by
 acting on the array itself?

Please double check what the patch does, test it, and think again
about this question (hint: pos != key) :)

Cheers,

 $input = array(3, 'bar', 'baz');
 echo $input[2]; // returns 'baz'
 echo $input[0]; // returns 3
 echo $input[5]; // returns NULL, Notice: Undefined offset: 5

 --Dan

 --
  T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
  4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409

 --
 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



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Derick Rethans
On Wed, 17 Mar 2010, Daniel Convissor wrote:

  $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
 
 Why waste time calling a function when this can be done right now by 
 acting on the array itself?

Because array keys can be sparse and don't have to be in order.

regards,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Daniel Convissor
On Wed, Mar 17, 2010 at 04:07:36PM +0100, Pierre Joye wrote:
 
 Please double check what the patch does, test it, and think again
 about this question (hint: pos != key) :)

And once again, skimming emails (can you say ADD? I knew you could.) 
bites me in the tush. :)  I just looked at the first sentence and the 
code.  Missed the second sentence.

Thanks,

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
data intensive web and database programming
http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Patrick ALLAERT
2010/3/16 Felix De Vliegher felix.devlieg...@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?

Cheers,
Patrick

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Felix De Vliegher
On 17-mrt-2010, at 17:00, Patrick ALLAERT wrote:

 2010/3/16 Felix De Vliegher felix.devlieg...@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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Frederic Hardy

2010/3/16 Felix De Vliegher felix.devlieg...@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


Hello Felix !

Why not use arrayIterator::seek() ?

Best regards,
Fred
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Patrick ALLAERT
 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.

You could always use reset() in combination with next($int). The main
benefit I see doing so is that you can do something like:

reset($array);
$jump = 3;
while ($elem = next($jump)) {
// Some treatment that needs to be done on every $jump element
}

Which would be slower and less readable with array_seek() of course,
depends on your use case!

 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).

Well, it wouldn't be a mandatory option of course for BC. It would
jump forward/backward X elements just where X is 1 by default. It
could make sense to have both maybe?

Let's wait other's opinion.

Cheers,
Patrick

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Felix De Vliegher
On 17-mrt-2010, at 17:27, Frederic Hardy wrote:
 
 Hello Felix !
 
 Why not use arrayIterator::seek() ?
 
 Best regards,
 Fred

Because the functionality isn't exactly the same. ArrayIterator::seek() only 
sets the array pointer, array_seek would also return the value + have 
fseek()-like functionality with the SEEK_* consts and optional negative offsets.

Felix
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Derick Rethans
On Wed, 17 Mar 2010, Felix De Vliegher wrote:

 On 17-mrt-2010, at 17:27, Frederic Hardy wrote:
  
  Why not use arrayIterator::seek() ?
 
 Because the functionality isn't exactly the same. 
 ArrayIterator::seek() only sets the array pointer, array_seek would 
 also return the value + have fseek()-like functionality with the 
 SEEK_* consts and optional negative offsets.

To be honest, I'd rather have the proposed array_seek() return a status 
whether the seek worked or not. Notices are uncool and you can already 
retrieve data/key with key() and current(). 

with kind regards,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Felix De Vliegher
On 17-mrt-2010, at 17:52, Derick Rethans wrote:

 On Wed, 17 Mar 2010, Felix De Vliegher wrote:
 
 On 17-mrt-2010, at 17:27, Frederic Hardy wrote:
 
 Why not use arrayIterator::seek() ?
 
 Because the functionality isn't exactly the same. 
 ArrayIterator::seek() only sets the array pointer, array_seek would 
 also return the value + have fseek()-like functionality with the 
 SEEK_* consts and optional negative offsets.
 
 To be honest, I'd rather have the proposed array_seek() return a status 
 whether the seek worked or not. Notices are uncool and you can already 
 retrieve data/key with key() and current(). 
 

Update: http://phpbenelux.eu/array_seek-return.patch.txt
I've kept the fseek()-style return values (0 when fine, -1 when seek fails)

Cheers,
Felix

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Derick Rethans
On Wed, 17 Mar 2010, Felix De Vliegher wrote:

 On 17-mrt-2010, at 17:52, Derick Rethans wrote:
 
  On Wed, 17 Mar 2010, Felix De Vliegher wrote:
  
  On 17-mrt-2010, at 17:27, Frederic Hardy wrote:
  
  Why not use arrayIterator::seek() ?
  
  Because the functionality isn't exactly the same. 
  ArrayIterator::seek() only sets the array pointer, array_seek would 
  also return the value + have fseek()-like functionality with the 
  SEEK_* consts and optional negative offsets.
  
  To be honest, I'd rather have the proposed array_seek() return a status 
  whether the seek worked or not. Notices are uncool and you can already 
  retrieve data/key with key() and current(). 
  
 
 Update: http://phpbenelux.eu/array_seek-return.patch.txt
 I've kept the fseek()-style return values (0 when fine, -1 when seek fails)

Any reason why you picked that over the (IMO more logical) true/false 
approach?

with kind regards,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Felix De Vliegher
On 17-mrt-2010, at 19:09, Derick Rethans wrote:

 On Wed, 17 Mar 2010, Felix De Vliegher wrote:
 
 On 17-mrt-2010, at 17:52, Derick Rethans wrote:
 
 On Wed, 17 Mar 2010, Felix De Vliegher wrote:
 
 On 17-mrt-2010, at 17:27, Frederic Hardy wrote:
 
 Why not use arrayIterator::seek() ?
 
 Because the functionality isn't exactly the same. 
 ArrayIterator::seek() only sets the array pointer, array_seek would 
 also return the value + have fseek()-like functionality with the 
 SEEK_* consts and optional negative offsets.
 
 To be honest, I'd rather have the proposed array_seek() return a status 
 whether the seek worked or not. Notices are uncool and you can already 
 retrieve data/key with key() and current(). 
 
 
 Update: http://phpbenelux.eu/array_seek-return.patch.txt
 I've kept the fseek()-style return values (0 when fine, -1 when seek fails)
 
 Any reason why you picked that over the (IMO more logical) true/false 
 approach?

No, just to keep it similar to fseek(). Either way works for me.

Felix
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-17 Thread Brian Moon

Update: http://phpbenelux.eu/array_seek-return.patch.txt
I've kept the fseek()-style return values (0 when fine, -1 when seek fails)


Any reason why you picked that over the (IMO more logical) true/false
approach?


No, just to keep it similar to fseek(). Either way works for me.


I would expect it to return the same as current, next, prev rather than 
a file io function.


Brian.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DEV] array_seek function

2010-03-16 Thread Pierre Joye
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
felix.devlieg...@gmail.com 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



Re: [PHP-DEV] array_seek function

2010-03-16 Thread Felix De Vliegher
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
 felix.devlieg...@gmail.com 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


Re: [PHP-DEV] array_seek function

2010-03-16 Thread Richard Quadling
On 16 March 2010 13:30, Felix De Vliegher felix.devlieg...@gmail.com 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


Maybe not as efficient as it could be but ...

?php
$input = array('One' = 'Itchy', 'Two' = 'Knee', 'Three' = 'San',
'Four' = 'She');

echo @reset(array_keys(array_values($input), 'Knee'));

Richard.
-- 
-
Richard Quadling
Standing on the shoulders of some very clever giants!
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-16 Thread Derick Rethans
On Tue, 16 Mar 2010, Felix De Vliegher wrote:

 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 :)

Does it also seek the array pointer? Because I think array_seek that 
moves the pointer,  in combination with current() and key() might make 
slightly more sense?

with kind regards,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-16 Thread Felix De Vliegher
On 16-mrt-2010, at 17:07, Derick Rethans wrote:

 On Tue, 16 Mar 2010, Felix De Vliegher wrote:
 
 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 :)
 
 Does it also seek the array pointer? Because I think array_seek that 
 moves the pointer,  in combination with current() and key() might make 
 slightly more sense?
 

Mikko updated the patch a bit to set the array pointer correctly (and make it 
perform a bit better when dealing with large arrays), my version left it one 
position too far. So yes, that's possible. The updated version can be found 
here: http://valokuva.org/~mikko/array_seek.patch.txt

Cheers,
Felix

Re: [PHP-DEV] array_seek function

2010-03-16 Thread Derick Rethans
On Tue, 16 Mar 2010, Felix De Vliegher wrote:

 On 16-mrt-2010, at 17:07, Derick Rethans wrote:
 
  On Tue, 16 Mar 2010, Felix De Vliegher wrote:
  
  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 :)
  
  Does it also seek the array pointer? Because I think array_seek that 
  moves the pointer,  in combination with current() and key() might make 
  slightly more sense?
 
 Mikko updated the patch a bit to set the array pointer correctly (and 
 make it perform a bit better when dealing with large arrays), my 
 version left it one position too far. So yes, that's possible. The 
 updated version can be found here: 
 http://valokuva.org/~mikko/array_seek.patch.txt

I was also thinking, can we just make this work just like fseek (with a 
whence parameter) as well? (http://uk3.php.net/fseek)

with kind regards,
Derick
-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-16 Thread Mikko Koppanen
On Tue, Mar 16, 2010 at 4:22 PM, Derick Rethans der...@php.net wrote:
 I was also thinking, can we just make this work just like fseek (with a
 whence parameter) as well? (http://uk3.php.net/fseek)

Hi,

not sure how SEEK_END is supposed to work with arrays but here is
SEEK_SET and SEEK_CUR (with positive and negative offset)
http://valokuva.org/~mikko/array_seek_whence.patch.txt

-- 
Mikko Koppanen

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-16 Thread Stanislav Malyshev

Hi!


Right now, it returns the value of a given position.


How it's different from:

array_slice() returns the sequence of elements from the array array  as 
specified by the offset  and length  parameters?

--
Stanislav Malyshev, Zend Software Architect
s...@zend.com   http://www.zend.com/
(408)253-8829   MSN: s...@zend.com

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-16 Thread Brian Moon

Right now, it returns the value of a given position.


How it's different from:

array_slice() returns the sequence of elements from the array array as
specified by the offset and length parameters?


array_slice returns an array of elements. This function would return the 
value at the given position.


Brian.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DEV] array_seek function

2010-03-16 Thread Jared Williams
 

 -Original Message-
 From: Felix De Vliegher [mailto:felix.devlieg...@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


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] array_seek function

2010-03-16 Thread Felix De Vliegher
Hi

As SEEK_END only makes sense with zero or negative offsets (for arrays anyway), 
I've come up with an implementation for SEEK_END:
http://phpbenelux.eu/array_seek.patch.txt

So you can do:
$arr = array('a', 'b', 'c', 'd');
echo array_seek($arr, -2, SEEK_END); // outputs 'b'
echo array_seek($arr, 0, SEEK_END); // outputs 'd'


Cheers,
Felix

On 16-mrt-2010, at 19:07, Mikko Koppanen wrote:

 On Tue, Mar 16, 2010 at 4:22 PM, Derick Rethans der...@php.net wrote:
 I was also thinking, can we just make this work just like fseek (with a
 whence parameter) as well? (http://uk3.php.net/fseek)
 
 Hi,
 
 not sure how SEEK_END is supposed to work with arrays but here is
 SEEK_SET and SEEK_CUR (with positive and negative offset)
 http://valokuva.org/~mikko/array_seek_whence.patch.txt
 
 -- 
 Mikko Koppanen


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php