php循环第二次时添加自定义data 属性

<?php foreach($header as $rowItem): ?>
<tr>
    <?php foreach($rowItem as $headerItem): ?>
        <th data-tablesaw-priority="persist" id="<?= $headerItem['id'] ?>" class="<?= $headerItem['class'] ?>"
        <?= (isset($headerItem['colspan']))?'colspan="'. $headerItem['colspan'] .'"':'' ?>
        <?= (isset($headerItem['rowspan']))?'rowspan="'. $headerItem['rowspan'] .'"':'' ?>
        <?= (isset($headerItem['style']))?'style="'. $headerItem['style'] .'"':'' ?> >
            <?= $headerItem['text'] ?>
        </th>
    <?php endforeach ?>
</tr>

<?php endforeach ?>

此循环会有三个<tr> 标签,现在需要在第二个<tr>标签里的<th>添加自定义属性。这需要如何编写?

clipboard.png

阅读 2.6k
2 个回答

方法一,使用foreach,新增一个count变量,记录循环次数,如果$count == 1,接下来做该做的事

方法二,使用for,在$i==1时做该做的事

for($i=0;$i<count($header);$i++)
{
    if($i === 1) {
        //
    }else{
        //
    }
}

最简单的,if判断循环出来的key

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题