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!
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.
$firstrow = true;
foreach ($result as $key => $value)
{
if ($firstrow) {
// something on only the first row
$firstrow = false;
}
// something every row
}
?>
$firstvalue = $result[0];
// something on only the first row
unset($result[0]);
foreach ($result as $key => $value)
{
// something every other row
}
?>