Re: foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux