|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 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 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' |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Aug 29, 2:40pm, mdw1...@mdw1982.com (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 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 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' Is it a numeric array (i.e. not an associative array)? If so, you can use foreach ($array as $index => $value) |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 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 me code a solution? > > thanks, > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Fri, Aug 29, 2008 at 2:40 PM, Mark Weaver <mdw1982@mdw1982.com> 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 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 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 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." |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 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 me code a solution? Try looking at "current" http://www.php.net/current Maybe that will . Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 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' |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Dan Joseph schreef:
> On Fri, Aug 29, 2008 at 2:40 PM, Mark Weaver <mdw1982@mdw1982.com> wrote: > .... >> > This may also 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 > |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 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 me code a solution? > > Try looking at "current" > > http://www.php.net/current > > Maybe that will . > > Cheers, > > tedd > > > Thanks all for the ful 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' |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Mark Weaver schreef:
> tedd wrote: .... >> >> Try looking at "current" >> >> http://www.php.net/current >> >> Maybe that will . 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 > |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|