Foreach & Array related

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Foreach & Array related

Post by Shendemiar »

Code: Select all

<?php
foreach ($result as $key => $value)
{
something here
}
?>
How can i check if i'm on the first element of the loop? (assuming there is more than 0 elements in the array)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$keys = array_keys($result);
foreach($result as $key => $value)
{
  if($key == $keys[0]) // first element?
  {}
  else
  {}
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

The foreach loop automatically starts on the first key. It creates and uses it's own pointer in the array, so there's no need (that I can think of - please enlighten me if I'm wrong) to double check if your starting at the beginning.
PHP Manual wrote: Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.

Note: Also note that foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array.
Manual page: [php_man]foreach[/php_man]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think it's more that Shendemiar wants to do some differing or specialized processing on the first element, and not on the following ones.
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

feyd wrote:I think it's more that Shendemiar wants to do some differing or specialized processing on the first element, and not on the following ones.
Yes thats correct. Thanks for the hint.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

$n = 0;
foreach ($result as $key => $value)
{
    if ($n == 0) {
// something on only a specific row
    }
// something here
    ++$n'
}
?>
Or if you only care about the first row:

Code: Select all

$firstrow = true;
foreach ($result as $key => $value)
{
    if ($firstrow) {
// something on only the first row
        $firstrow = false;
    }
// something every row
}
?>
Or for speed:

Code: Select all

$firstvalue = $result[0];
// something on only the first row
unset($result[0]);
foreach ($result as $key => $value)
{
// something every other row
}
?>
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Heh, thanks. Actually quite handy examples for all-array related looping :D
Post Reply