By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,406 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,406 IT Pros & Developers. It's quick & easy.

Iterator first item?

P: n/a
cjl
Hey all:

Let's say I have the code:

foreach($data as $row)
{
do something with each $row
}

Is there a way to do something specific to the first item iterated?
Pseudocode:

foreach($data as $row)
{
if first time through iteration do something with $row
else do something which each remaining $row
}

Is this possible? I think I could use a regular 'for' loop, using the
length of the $data array as a guide, and then an 'if..else' structure
to see if it is the first time through the 'for' loop, but I was
looking for a more elegant solution.

-CJL

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You could start a count, and for each time it loops add one up.
When $count == 1, do something, if else do nothing?

Jul 17 '05 #2

P: n/a
Well, there's always the "old" way of going through an array using
reset() and each().

$first = reset($data);
// do stuff with first item
next($data);
while(list($index, $item) = each($data)) {
// do stuff with the rest
}

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.