1 回答
TA贡献1816条经验 获得超6个赞
这可能最容易通过将数组拆分为块并在循环中单独处理每个块,使用count块的 来决定将哪个类添加到跨度:
$array = range(1, 11);
$chunks = array_chunk($array, 4);
$span_classes = ['', 'one-half', 'one-third', 'one-fourth'];
foreach ($chunks as $chunk) {
echo "<div class=\"item\">\n";
$count = count($chunk);
$span_class = $span_classes[$count-1];
for ($i = 0; $i < $count; $i++) {
$last = $i == $count - 1 ? ' last' : '';
$val = $chunk[$i];
echo "<span class=\"$span_class$last\">$val</span>\n";
}
echo "</div>\n";
}
输出:
<div class="item">
<span class="one-fourth">1</span>
<span class="one-fourth">2</span>
<span class="one-fourth">3</span>
<span class="one-fourth last">4</span>
</div>
<div class="item">
<span class="one-fourth">5</span>
<span class="one-fourth">6</span>
<span class="one-fourth">7</span>
<span class="one-fourth last">8</span>
</div>
<div class="item">
<span class="one-third">9</span>
<span class="one-third">10</span>
<span class="one-third last">11</span>
</div>
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报