Php 使用foreach循环实现多列表

Php 使用foreach循环实现多列表,php,html,foreach,Php,Html,Foreach,我想实现一个使用foreach循环创建三列表的逻辑。示例代码如下所示 $array = ['0','1','2','3','4','5','6']; $table = '<table class="table"><tbody>'; foreach($array as $a=>$v){ //if 0th or 3rd???????????wht should be here? $table .= '<tr>'; $table

我想实现一个使用foreach循环创建三列表的逻辑。示例代码如下所示

$array = ['0','1','2','3','4','5','6'];
$table = '<table class="table"><tbody>';

foreach($array as $a=>$v){

    //if 0th or 3rd???????????wht should be here?
    $table .= '<tr>';

    $table .= '<td>$v</td>';

    //if 2nd or 5th??????????and here too???
    $table .= '</tr>';

}

$table = '</tbody></table>';            
$array=['0','1','2','3','4','5','6'];
$table='';
foreach($a=>v的数组){
//如果是第0次还是第3次,这里应该是什么?
$table.='';
$table.='$v';
//如果是2号还是5号?这里也是吗???
$table.='';
}
$table='';
有什么想法吗


预期输出是一个简单的3X3表,其中包含来自数组的值

<?php
echo('<table><tr>');
$i = 0;
foreach( $array as $product )
{
   $i++;
   echo '<td>'.$product .'</td>';
   if($i % 3==0)
   {
      echo '</tr><tr>';
   }
}
echo'</tr></table>';
?>

此处的结果:

<table>
   <tr>
      <td>data1</td>
      <td>data2</td>
      <td>data3</td>    
   </tr>    
   <tr>
     <td>data4</td>
     <td>data5</td>
     <td>data6</td>    
   </tr>
</table>

数据1
数据2
数据3
数据4
数据5
数据6

这应该适合您:

(请参阅我在foreach循环前后的开始和结束处添加了一个
tr
。此外,我还将引号更改为双引号,并确保将文本添加到所有位置。)


以下是一个简单的解决方案:


这是一个简单的增量解决方案,它与动态生成的表(如Magento产品视图页面中的表)一起工作,以帮助在两个表列中显示产品属性,前面是属性标签->实际上,我们有4个表列和属性标签。这对于具有多个属性且需要较长页面才能显示的产品非常有用

<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
if($_additional = $this->getAdditionalData()): ?>

<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="table table-bordered table-hover" id="product-attribute-specs-table">
    <col width="25%" />
    <col />
    <tbody>
    <?php $i = 1; ?>
    <tr>
    <?php foreach ($_additional as $_data): ?>
    <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>


            <th style="width: 20%;"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
            <td class="data" style="width:20%;"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
            <?php $i++; if($i > 1 && ($i & 1) ) echo "</tr><tr>";?>

    <?php } ?>
    <?php endforeach; ?>
    </tr>
    </tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>

装饰表('product-attribute-specs-table')

1维数组和3个collumns?为什么?怎样?什么?预期的输出是什么?我想你必须
echo$table
。。。您期望的o/p是什么?使用
array\u chunk()
是一个很好的解决方案。只是把你的例子写得简单一点
<table class='table'>
<tbody>
    <tr>
        <td>0</td>
        <td>1</td>
        <td>2</td>
    </tr>
    <tr>
        <td>3</td>
        <td>4</td>
        <td>5</td>
    </tr>
    <tr>
        <td>6</td>
    </tr>
</tbody>
</table>
<?php

    $array = array('0','1','2','3','4','5','6');
    $d = array_chunk($array, 3);

    $table = "<table border='1' class='table'><tbody>";
    foreach($d as $v)
        $table .= "<tr><td>" . implode("</td><td>", $v) . "</td></tr>";

    $table .= "</tbody></table>";
    echo $table;

?>
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
if($_additional = $this->getAdditionalData()): ?>

<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="table table-bordered table-hover" id="product-attribute-specs-table">
    <col width="25%" />
    <col />
    <tbody>
    <?php $i = 1; ?>
    <tr>
    <?php foreach ($_additional as $_data): ?>
    <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>


            <th style="width: 20%;"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
            <td class="data" style="width:20%;"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
            <?php $i++; if($i > 1 && ($i & 1) ) echo "</tr><tr>";?>

    <?php } ?>
    <?php endforeach; ?>
    </tr>
    </tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>