[PHP] Re: end of array
M. Sokolewicz [EMAIL PROTECTED] writes: Raj Shekhar wrote: M. Sokolewicz [EMAIL PROTECTED] writes: Raj Shekhar wrote: $n_elts = count($myarray); for ($i=0; $i $n_elts ; $i++) { if ($i = $n_elts -1) ^^^ Use of == required to make it work { echo On last element; break; } else { echo Somwhere in the middle; } } that's an eternal loop in case you hadn't noticed (*rolls eyes*) Oops :( not eternal loop though, only one loop why one? for($i=0; $i$n;$i++) { $i = ($n-1); } My statement was, if ($i = $n_elts -1) { echo On last element; break; } i.e. - assign ($i = $n_elts -1) and check the return value of the assignment. - If the assignment succeeds (which should, unless you are running short of free memory) THEN - echo - BREAK out of loop Since this conditions are met the first time the loop runs, the loop will run only once. I know I am correct, since I ran the code this time :P -- Raj Shekhar System Administrator, programmer and slacker home : http://rajshekhar.net blog : http://rajshekhar.net/blog/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: end of array
Jeffery Fernandez [EMAIL PROTECTED] writes: Hi all, I have a foreach loop on an array and within that loop I need to find if the array has reached the last pointer. I have tried if (next($row)) { } but that advances the pointer. Any tips on finding out if the array pointer has reached the last element ? $n_elts = count($myarray); for ($i=0; $i $n_elts ; $i++) { if ($i = $n_elts -1) { echo On last element; break; } else { echo Somwhere in the middle; } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: end of array
Raj Shekhar wrote: Jeffery Fernandez [EMAIL PROTECTED] writes: Hi all, I have a foreach loop on an array and within that loop I need to find if the array has reached the last pointer. I have tried if (next($row)) { } but that advances the pointer. Any tips on finding out if the array pointer has reached the last element ? $n_elts = count($myarray); for ($i=0; $i $n_elts ; $i++) { if ($i = $n_elts -1) { echo On last element; break; } else { echo Somwhere in the middle; } } that's an eternal loop in case you hadn't noticed (*rolls eyes*) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: end of array
M. Sokolewicz [EMAIL PROTECTED] writes: Raj Shekhar wrote: $n_elts = count($myarray); for ($i=0; $i $n_elts ; $i++) { if ($i = $n_elts -1) ^^^ Use of == required to make it work { echo On last element; break; } else { echo Somwhere in the middle; } } that's an eternal loop in case you hadn't noticed (*rolls eyes*) Oops :( not eternal loop though, only one loop -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: end of array
Raj Shekhar wrote: M. Sokolewicz [EMAIL PROTECTED] writes: Raj Shekhar wrote: $n_elts = count($myarray); for ($i=0; $i $n_elts ; $i++) { if ($i = $n_elts -1) ^^^ Use of == required to make it work { echo On last element; break; } else { echo Somwhere in the middle; } } that's an eternal loop in case you hadn't noticed (*rolls eyes*) Oops :( not eternal loop though, only one loop why one? for($i=0; $i$n;$i++) { $i = ($n-1); } to me that means the following: 1. check if $i$n; true! ($i=0; $n1) 2. $i=$n-1, this means that $i$n (less by 1 in fact) 3. check if $i$n; true! ($i=$n-1; $n$i by definition) 4. $i is reset to $n-1 5-eternity. conditions are met, and var is reset or am I missing something here? :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php