动态td foreach循环

时间:2016-10-08 13:18:14

标签: php arrays

我想将一个PHP数组显示为td中8个tr的表,但我不知道如何限制内循环中的td

 <?php
    $i=0;
    $related = array('1','2','3','4','5','6','7','8','9','10');
    for($i=0;$i<count($related);$i++){
        echo "<tr>";
        for($j=0;$j<8;$j++){
            echo "<td>".$related[$j]."</td>";
            $i++;   
        }
        echo $i;
        echo "</tr>";   
    }
?>

2 个答案:

答案 0 :(得分:0)

是的,我终于明白了,

/var/www/somepath/index.php

答案 1 :(得分:0)

<?php
    // your array
    $related = array('1','2','3','4','5','6','7','8','9','10');
    for($i=0;$i<count($related);$i++){
    $row = "";
    $row = "<tr>";
        for($j=0;$j<8;$j++){
        $row .= "<td>".$related[$j]."</td>";
    }
    $row .= "</tr>";
    echo $row;
}
?>

试试这段代码