Page 1 of 1

determine if in first itteration in foreach

Posted: Wed Aug 02, 2006 9:33 am
by jmut
how could I determine if the first itteration of foreach.

I now how to determine if last...but first? :roll:

Code: Select all

//determine if in last itteration

foreach ($array as $elem) {
 if (!next($array)) {
 //this is last itteration. no more.
 }
}
Of course I can add a variable or something. I would rather not use weird solutions.

Posted: Wed Aug 02, 2006 9:36 am
by feyd

Code: Select all

$first = true;
foreach ($array as $elem)
{
  if ($first)
  {
    // do something
    $first = false;
  }
}
Image

Posted: Wed Aug 02, 2006 9:45 am
by jmut
feyd wrote:

Code: Select all

$first = true;
foreach ($array as $elem)
{
  if ($first)
  {
    // do something
    $first = false;
  }
}
Image
gr....ok I thought I don't need variable. But guess this is as perfect as it gets :)
10x

Posted: Wed Aug 02, 2006 9:51 am
by RobertGonzalez
feyd wrote:

Code: Select all

$first = true;
foreach ($array as $elem)
{
  if ($first)
  {
    // do something
    $first = false;
  }
}
Image
I cannot believe that for three years this logic has evaded me. Damn, where have I been?
Image

Posted: Wed Aug 02, 2006 9:53 am
by feyd
Everah wrote:Damn, where have I been?
Image

Posted: Wed Aug 02, 2006 10:24 am
by RobertGonzalez
Pimps smilies are going to be the end of me, I know it.
Image

Posted: Wed Aug 02, 2006 10:30 am
by Luke
where the hell are you guys getting those?

Posted: Wed Aug 02, 2006 8:09 pm
by RobertGonzalez
The little yellow Walmart rejects are residing comfortably in a corner of Modville. Right next to the Oompa Loompas.