foreach in foreach php两个数组

时间:2015-02-04 21:39:15

标签: php arrays foreach html-table

我的问题是我的代码总是从第一个位置开始。所以我不能给每行输入相关id的输入字段。

foreach ($phase as $line) {
echo "<tr>";
    echo "<td>";
        echo "<div class='headliner-th'><b>".$line['title']."</b></div>";
    echo "</td>";?>
    <?php foreach($items as $i=>$item): ?>
        <td><?= $form->field($item,"[$i]content")->textInput(['maxlength' => 200])->label(false); ?></td>
    <?php endforeach; ?>
<?php echo "</tr>";
}

那么当涉及新行时,如何避免再次获得相同的输入字段?

2 个答案:

答案 0 :(得分:1)

内部foreach循环始终循环遍历同一变量$items。难道不是外面的foreach以某种方式循环的东西吗?

答案 1 :(得分:0)

我做了多个if case。它似乎有点脏,但比骚扰我的对象更容易。所以它看起来像这样:我希望任何人都可以使用它:首先在行上排序对象而不是在col上排序

非常重要
<table class="sal-list" style="width: 100%;">
<tr>
<?php
    $width = 100/(count($pos)+1);
    echo "<th style='width:".$width."%;'></th>";
    foreach ($pos as $item) {
        echo "<th style='width:".$width."%;'><div class='headliner-th'>".$item['title']."</div></th>";
    }
?>
</tr>
<?php
$g = 0;
foreach ($phase as $line) {
    echo "<tr>";
        echo "<td>";
            echo "<div class='headliner-th'><b>".$line['title']."</b></div>";
        echo "</td>";?>
            <?php foreach($items as $i=>$item): ?>
                <?php if (($g <= count($items)) and ($g == $i) and ($line['id'] == $item['phas_id'])) : ?>
                    <td><?= $form->field($item,"[$i]content")->textInput(['maxlength' => 200])->label(false); ?></td>
                <?php $g++; endif;?>
            <?php endforeach; ?>

        <?php
    echo "</tr>";
}
?>