Re: foreach

From: Date: Wed, 11 Oct 2006 07:24:52 +0000
Subject: Re: foreach
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-242959@lists.php.net to get a copy of this message
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

« previous php.general (#242959) next »