[PHP] Testing for Current pointer position in array during iteration
Hi All, Go figure... I sat down today to get some more work on my current project; I got to a certain point where I need to step through an array with a foreach loop. I found that I need to test for the current pointer position of the array, but I haven't a clue as to how this might be accomplished. I've been all over google and php.net, but I'm not finding anything that will help me do this. Basically all I want to do is something like this: if( current_arrayPointer_postition == n ){ ... do this }else{ ...do this } Anyone know of a built-in PHP function that can assist with this, or can you point me to a resource that could help me code a solution? thanks, -- Mark It was good to be the fire... Better by far than to crawl and mew and suck and $h1t and die! 'Arthur C. Clarke' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Testing for Current pointer position in array during iteration
Mark Weaver schreef: Hi All, Go figure... I sat down today to get some more work on my current project; I got to a certain point where I need to step through an array with a foreach loop. I found that I need to test for the current pointer position of the array, but I haven't a clue as to how this might be accomplished. I've been all over google and php.net, but I'm not finding anything that will help me do this. Basically all I want to do is something like this: if( current_arrayPointer_postition == n ){ ... do this }else{ ...do this } foreach isn't really like that. it uses (IIRC) an internal pointer of it's own leaving the userland pointer where ever it is. but: $n = 5; // make a new array with numeric keys starting at 0 $array = array_values(array); // loop it foreach ($array as $key = $val) { if ($key == $n) { echo I got five on it; } else { echo usual suspect; } } Anyone know of a built-in PHP function that can assist with this, or can you point me to a resource that could help me code a solution? thanks, -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Testing for Current pointer position in array during iteration
On Fri, Aug 29, 2008 at 2:40 PM, Mark Weaver [EMAIL PROTECTED] wrote: Hi All, Go figure... I sat down today to get some more work on my current project; I got to a certain point where I need to step through an array with a foreach loop. I found that I need to test for the current pointer position of the array, but I haven't a clue as to how this might be accomplished. I've been all over google and php.net, but I'm not finding anything that will help me do this. Basically all I want to do is something like this: if( current_arrayPointer_postition == n ){ ... do this }else{ ...do this } Anyone know of a built-in PHP function that can assist with this, or can you point me to a resource that could help me code a solution? thanks, -- Mark It was good to be the fire... Better by far than to crawl and mew and suck and $h1t and die! 'Arthur C. Clarke' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php This may also help as well as Jochem's suggestion: http://us2.php.net/key -- -Dan Joseph www.canishosting.com - Plans start @ $1.99/month. Build a man a fire, and he will be warm for the rest of the day. Light a man on fire, and will be warm for the rest of his life.
Re: [PHP] Testing for Current pointer position in array during iteration
At 2:40 PM -0400 8/29/08, Mark Weaver wrote: Hi All, Go figure... I sat down today to get some more work on my current project; I got to a certain point where I need to step through an array with a foreach loop. I found that I need to test for the current pointer position of the array, but I haven't a clue as to how this might be accomplished. I've been all over google and php.net, but I'm not finding anything that will help me do this. Basically all I want to do is something like this: if( current_arrayPointer_postition == n ){ ... do this }else{ ...do this } Anyone know of a built-in PHP function that can assist with this, or can you point me to a resource that could help me code a solution? Try looking at current http://www.php.net/current Maybe that will help. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Testing for Current pointer position in array during iteration
Dan Joseph schreef: On Fri, Aug 29, 2008 at 2:40 PM, Mark Weaver [EMAIL PROTECTED] wrote: ... This may also help as well as Jochem's suggestion: http://us2.php.net/key not unless you wan't a headache (re-read the bit about foreach's internal pointer): php -r '$r = array(a,b,c); next($r); $K = key($r); foreach ($r as $k = $v) { echo $K , key($r), $k\n; }' outputs: 1 1 0 1 1 1 1 1 2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Testing for Current pointer position in array during iteration
Jochem Maas wrote: Mark Weaver schreef: Hi All, Go figure... I sat down today to get some more work on my current project; I got to a certain point where I need to step through an array with a foreach loop. I found that I need to test for the current pointer position of the array, but I haven't a clue as to how this might be accomplished. I've been all over google and php.net, but I'm not finding anything that will help me do this. Basically all I want to do is something like this: if( current_arrayPointer_postition == n ){ ... do this }else{ ...do this } foreach isn't really like that. it uses (IIRC) an internal pointer of it's own leaving the userland pointer where ever it is. but: $n = 5; // make a new array with numeric keys starting at 0 $array = array_values(array); // loop it foreach ($array as $key = $val) { if ($key == $n) { echo I got five on it; } else { echo usual suspect; } } very nice... Thank you Jochem. That did the trick. -- Mark It was good to be the fire... Better by far than to crawl and mew and suck and shit and die! 'Arthur C. Clarke' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Testing for Current pointer position in array during iteration
tedd wrote: At 2:40 PM -0400 8/29/08, Mark Weaver wrote: Hi All, Go figure... I sat down today to get some more work on my current project; I got to a certain point where I need to step through an array with a foreach loop. I found that I need to test for the current pointer position of the array, but I haven't a clue as to how this might be accomplished. I've been all over google and php.net, but I'm not finding anything that will help me do this. Basically all I want to do is something like this: if( current_arrayPointer_postition == n ){ ... do this }else{ ...do this } Anyone know of a built-in PHP function that can assist with this, or can you point me to a resource that could help me code a solution? Try looking at current http://www.php.net/current Maybe that will help. Cheers, tedd Thanks all for the helpful suggestions. Actually Jochem's suggestion was just what I needed. I was stuck at just how to test where the pointer was and couldn't see a way out. I knew there had to be a simple, elegant solution, but apparently was over-thinking it. Jochem put things back into perspective for me. -- Mark It was good to be the fire... Better by far than to crawl and mew and suck and shit and die! 'Arthur C. Clarke' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Testing for Current pointer position in array during iteration
Mark Weaver schreef: tedd wrote: ... Try looking at current http://www.php.net/current Maybe that will help. not in the context of foreach() (I'll repeat the oneliner here because tedd's old and has a crap email client ;-): php -r '$r = array(a,b,c); next($r); $K = key($r); foreach ($r as $k = $v) { echo $K , key($r), $k\n; }' current() is akin to key() in that they both work on where the array pointer currently is. not that current() returns a key ;-) Jochem put things back into perspective for me. I'm having that printed on a T-Shirt :-P -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Testing for Current pointer position in array during iteration
At 11:10 PM +0200 8/29/08, Jochem Maas wrote: Mark Weaver schreef: Jochem put things back into perspective for me. I'm having that printed on a T-Shirt :-P Damn! You get more T-Shirts than me. :-) Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php