下面是我的多维数组代码,将其显示为具有交替行颜色的表。
<?php
$Mags = array(
array(
'1', 'Krish','1977/03','V10', 'M3','March-77'
),
array(
'2', 'Vansh','1978/09','V11', 'M9','Sept-78'
),
array(
'3', 'Sushi','1981/07','V14', 'M7','July-81'
)
);
?>
<table id="fancytable"><tbody><thead>
<tr>
<th>S.No</th>
<th>Dedicate</th>
<th>Vol.</th>
<th>ID</th>
<th>Month-Year</th>
</tr></thead>
<?php
$i=1;
foreach($Mags as $mag)
{
if($i % 2 == 0)
{
echo "<tr class='odd'>";
}
else
{
echo "<tr>";
}
foreach($mag as $item)
{
echo "<td>$item</td>";
}
echo '</tr>';
$i = $i + 1;
}
?>
</tbody></table>
我不想为每个项目单独提供“密钥”。因为我正在从Excel“csv”文件复制数组项(即每行有6列) 我想每行只显示5个主题。产品编号“3”应该用作产品编号“2”的相对URL(base-url为“http://mydomain.com/”)。
我的问题是: 我如何使用“foreach”php函数?
答案 0 :(得分:0)
你做不到。只需使用常规for循环:
for ($x = 0; $x < count($Mags); $x++) {
if (isset($Mags[$x - 1])) {
$Mags[$x]['hyperlink'] = $Mags[$x - 1];
}
}