Re: foreach

From: Date: Thu, 12 Oct 2006 13:09:40 +0000
Subject: Re: foreach
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-243022@lists.php.net to get a copy of this message
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? >

« previous php.general (#243022) next »