Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

foreach

0 views
Skip to first unread message

João Cândido de Souza Neto

unread,
Oct 10, 2006, 2:14:25 PM10/10/06
to php-g...@lists.php.net
Hello.

In the follow code:

$numbers=array(1,2,3,4,5);
foreach ($numbers as number) {
...
}

Inside foreach, could i know if i am in the last element of the array
$numbers?

--
João Cândido de Souza Neto
Curitiba Online
jo...@curitibaonline.com.br
(41) 3324-2294 (41) 9985-6894
http://www.curitibaonline.com.br

Brad Bonkoski

unread,
Oct 10, 2006, 2:18:41 PM10/10/06
to João Cândido de Souza Neto, php-g...@lists.php.net
João Cândido de Souza Neto wrote:
> Hello.
>
> In the follow code:
>
> $numbers=array(1,2,3,4,5);
> foreach ($numbers as number) {
> ...
> }
>
> Inside foreach, could i know if i am in the last element of the array
> $numbers?
>
>
Sure, maintain a count in the foreach and then compare to
count($numbers)..but why not just use a for loop?

John Nichel

unread,
Oct 10, 2006, 2:26:10 PM10/10/06
to php-g...@lists.php.net
João Cândido de Souza Neto wrote:
> Hello.
>
> In the follow code:
>
> $numbers=array(1,2,3,4,5);
> foreach ($numbers as number) {
> ...
> }
>
> Inside foreach, could i know if i am in the last element of the array
> $numbers?
>

$last = end ( $numbers );
reset ( $numbers );
foreach ( $numbers as $number ) {
if ( $number == $last ) {
// last element
...
}
....
}

--
John C. Nichel IV
Programmer/System Admin (ÜberGeek)
Dot Com Holdings of Buffalo
716.856.9675
jni...@dotcomholdingsofbuffalo.com

John Nichel

unread,
Oct 10, 2006, 2:34:54 PM10/10/06
to php-g...@lists.php.net
Chris Boget wrote:
>> $last = end ( $numbers );
>> reset ( $numbers );
>
> I thought foreach() already performed a reset()? Why do it again here?
>

Well, corn my fritters, according to TFM, it does this indeed. Maybe an
old dog can learn new tricks. ;)

Chris Boget

unread,
Oct 10, 2006, 2:29:58 PM10/10/06
to John Nichel, php-g...@lists.php.net
> $last = end ( $numbers );
> reset ( $numbers );

I thought foreach() already performed a reset()? Why do it again here?

thnx,
Chris

João Cândido de Souza Neto

unread,
Oct 10, 2006, 2:24:36 PM10/10/06
to php-g...@lists.php.net
You´re right, in my example here i could just use a for loop.

But in my real code it´s better to use foreach.

Thanks for your tip.

--

João Cândido de Souza Neto

Curitiba Online
jo...@curitibaonline.com.br
(41) 3324-2294 (41) 9985-6894
http://www.curitibaonline.com.br

"Brad Bonkoski" <bbon...@mediaguide.com> escreveu na mensagem
news:452BE40...@mediaguide.com...

Richard Lynch

unread,
Oct 10, 2006, 5:01:09 PM10/10/06
to João Cândido de Souza Neto, php-g...@lists.php.net
On Tue, October 10, 2006 1:14 pm, João Cândido de Souza Neto wrote:
> $numbers=array(1,2,3,4,5);
> foreach ($numbers as number) {
> ...
> }
>
> Inside foreach, could i know if i am in the last element of the array
> $numbers?

In general, you can't.

If the elements are unique, you could do:

$last = end($numbers);
...
if ($last == $number) "this is the end";

Most people who want this "feature" are trying to suppress outputting
a comma or other separator after the last item because they have not
yet figured out how to use this function:
http://php.net/implode

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

Ivo F.A.C. Fokkema

unread,
Oct 11, 2006, 3:24:52 AM10/11/06
to php-g...@lists.php.net
On Tue, 10 Oct 2006 14:34:54 -0400, John Nichel wrote:

> Chris Boget wrote:
>>> $last = end ( $numbers );
>>> reset ( $numbers );
>>
>> I thought foreach() already performed a reset()? Why do it again here?
>>
>
> Well, corn my fritters, according to TFM, it does this indeed. Maybe an
> old dog can learn new tricks. ;)

Actually, foreach() creates a copy of the mentioned array, and the array
pointer of the original array is not modified. Very useful to know if you
append entries to the array you're foreach()-ing and pulling your hair out
why foreach() doesn't iterate through those new entries...

An way of going through the array independent of the actual values (since
using end() relies on unique values):

$n = count($numbers);
$i = 0;
foreach ($numbers as $number) {
$i ++;
if ($i == $n) {
// Last element...
} else {
// Not the last element...
}
}

Ivo

João Cândido de Souza Neto

unread,
Oct 10, 2006, 5:09:37 PM10/10/06
to php-g...@lists.php.net
Thanks for your tip, but unfortunately it wouldn´t help me.


""Richard Lynch"" <c...@l-i-e.com> escreveu na mensagem
news:11949.208.195.234.25...@www.l-i-e.com...

Jochem Maas

unread,
Oct 12, 2006, 9:09:40 AM10/12/06
to João Cândido de Souza Neto, php-g...@lists.php.net
João Cândido de Souza Neto wrote:
> Hello.
>
> In the follow code:
>
> $numbers=array(1,2,3,4,5);
> foreach ($numbers as number) {
> ...
> }
>
> Inside foreach, could i know if i am in the last element of the array
> $numbers?

you've already had a few alternatives; how about this:

foreach ($numbers as $key => $number) {
// loopy stuff
}
// do something special with the last item (and it's key?)
// which will currently be stored in $number and $key respectively
// e.g. :
processWhateverTheLastElementWas($numbers[ $key ]);
// or :
processWhateverTheLastElementsValueWas($number);

basically immediately after the foreach loop you have the key and value
of the last element - chances are you therefore have what you need to do what you need. no?


>

0 new messages