
October 22nd, 2009, 05:30 AM
|
|
Me
|
|
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937

Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
|
|
a foreach loop keeps track of its own array pointer. meaning that things like next(), prev() or reset() don't really affect the array like you might think. Another option if the array uses numeric indexes is to use a for loop.
PHP Code:
$array = array('a','b','c','d');
//loop through an array adding 2 on each iteration while we are below the count of the array.
for($i=0;$i<count($array);$i+=2){
echo "<div>";
echo $array[$i];
echo " ";
if(isset($array[$i+1])) echo $array[$i+1];
echo "</div>\n";
."</div>";
}
If we knew more about what you were trying to do though, there might be a more specific answer that could help. Seems to me if you wanted to get specific details about the next row you could just keep a row count and if count is even, echo one thing, odd echo another.
PHP Code:
$array = array('a','b','c','d');
$cnt = 0;
foreach($array as $value){
if($cnt%2==0){
echo "<div>";
echo $value." ";
} else {
echo $value;
echo "</div>";
}
$cnt++;
}
that should do pretty much the same thing.
|