PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Testing for Current pointer position in array during iteration
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Testing for Current pointer position in array during iteration

Réponse
 
LinkBack Outils de la discussion
Vieux 29/08/2008, 20h40   #1
Mark Weaver
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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'

  Réponse avec citation
Vieux 29/08/2008, 20h56   #2
SLerman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Testing for Current pointer position in array during iteration

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)
  Réponse avec citation
Vieux 29/08/2008, 21h07   #3
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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,
>


  Réponse avec citation
Vieux 29/08/2008, 21h17   #4
Dan Joseph
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Testing for Current pointer position in array during iteration

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

  Réponse avec citation
Vieux 29/08/2008, 21h23   #5
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Testing for Current pointer position in array duringiteration

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
  Réponse avec citation
Vieux 29/08/2008, 21h26   #6
Mark Weaver
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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'

  Réponse avec citation
Vieux 29/08/2008, 21h27   #7
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 <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

>


  Réponse avec citation
Vieux 29/08/2008, 22h15   #8
Mark Weaver
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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'

  Réponse avec citation
Vieux 29/08/2008, 23h10   #9
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 .


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

>


  Réponse avec citation
Vieux 29/08/2008, 23h23   #10
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Testing for Current pointer position in array duringiteration

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16869 seconds with 18 queries