我有一个问题需要解决。我在PHP中有这个HTML布局:
<div class="container">
<div class="wrapper">
<span class="entry"></span>
<span class="entry"></span>
</div>
<div class="wrapper">
<span class="entry"></span>
<span class="entry"></span>
</div>
<div class="wrapper">
<span class="entry"></span>
<span class="entry"></span>
</div>
<div class="wrapper">
<span class="entry"></span>
<span class="entry"></span>
</div>
</div>
我在这里有一个列布局。这意味着我总是想将2个元素打包到一个包装器类中。我从数组中获取所有元素,我想用循环输出。我的想法是每2次传递一次才输出包装器,但这会产生以下输出:
<div class="wrapper">
<div class="wrapper">
<span class="entry"></span>
</div>
<span class="entry"></span>
<div class="wrapper">
<span class="entry"></span>
</div>
<span class="entry"></span>
<div class="wrapper">
<span class="entry"></span>
</div>
<span class="entry"></span>
<div class="wrapper">
<span class="entry"></span>
</div>
<span class="entry"></span>
</div>
这是我的代码:
<div class="container">
<?php
$loop_count = 0;
foreach ( $array as $item ) {
if ( $loop_count % 2 === 0 ) { ?>
<div class="wrapper">
<?php } ?>
<span class="entry"><?= $item ?></span>
<?php if ( $loop_count % 2 === 0 ) { ?>
</div>
<?php }
$loop_count++;
} ?>
</div>
我在图像中的预期输出:
倚天杖