I am spoiled in using foreach() with a known array. But now I'm coming into a situation where, for the sake of subtotal breaks, I need to know the NEXT record if it's present also. I want to see if the code below is optimal. The table row you see is NOT any resemblance of the table row(s) I'll be outputting, just a way to see that yes, currently I'm getting both the current and next record, or nothing at the end.
Are there any "gotchas" in my coding and is there any more elegant way to generate $record and $nextRecord in a loop? Thanks, my appreciation to those of you who know the internals of PHP so well.
$records=array(
array(
'country'=>'USA',
'state'=>'Texas',
'city'=>'Houston',
'address'=>'123 Main St.',
'total'=>45.00
),
array(
'country'=>'USA',
'state'=>'Texas',
'city'=>'Houston',
'address'=>'777 Elbow Grease',
'total'=>175.00
),
array(
'country'=>'USA',
'state'=>'Texas',
'city'=>'Austin',
'address'=>'334 Emmy Dr.',
'total'=>391.00
),
array(
'country'=>'USA',
'state'=>'Texas',
'city'=>'Austin',
'address'=>'1204 Marlton St.',
'total'=>485.00
)
);
?><table border="1"><?php
while(true){
$i++;
if(is_array($nextRecord)){
$record=$nextRecord;
@$nextRecord=current(each($records));
}else{
@$record=current(each($records));
@$nextRecord=current(each($records));
}
//just shows that I do have the current and next record
?><tr>
<td><?php
echo '<pre>';
print_r($record);
echo '</pre>';
?>
</td>
<td><?php
echo '<pre>';
print_r($nextRecord);
echo '</pre>';
?>
</td>
</tr><?php
if(!$nextRecord)break;
}
?></table><?php