Re: Testing for Current pointer position in array during iteration

From: Date: Fri, 29 Aug 2008 19:26:37 +0000
Subject: Re: Testing for Current pointer position in array during iteration
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-279008@lists.php.net to get a copy of this message
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'

« previous php.general (#279008) next »