如何通过生成列布局来输出 foreach 内的元素?

我有一个问题需要解决。我在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>

我在图像中的预期输出:

http://img.mukewang.com/62ec84e50001a6df16740540.jpg

烙印99
浏览 87回答 1
1回答

倚天杖

你的代码会有点复杂:<?php$array = [1,2,3];?><div class="container">&nbsp; &nbsp; <?php&nbsp; &nbsp; $loop_count = 0;&nbsp; &nbsp; foreach ( $array as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; // here you open `div.wrapper`&nbsp; &nbsp; &nbsp; &nbsp; if ( $loop_count % 2 === 0 ) { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="wrapper">&nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; &nbsp; &nbsp; <span class="entry"><?= $item ?></span>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // here you close `div.wrapper`&nbsp; &nbsp; &nbsp; &nbsp; if ( $loop_count % 2 === 1 ) { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <?php }&nbsp; &nbsp; &nbsp; &nbsp; $loop_count++;&nbsp; &nbsp; }&nbsp; &nbsp; // here you close `div.wrapper` if you have odd count of elements&nbsp; &nbsp; if ( $loop_count % 2 === 1 ) { ?>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; <?php }?></div>不太复杂的解决方案是将数组拆分为大小为 2 的块:<?php$array = [1,2,3,4];$chunks = array_chunk($array, 2);?><div class="container">&nbsp; &nbsp; <?php&nbsp; &nbsp; foreach ( $chunks as $chunk ) {?>&nbsp; &nbsp; &nbsp; &nbsp; <div class="wrapper">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="entry"><?= $chunk[0] ?></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Here you can check if second element exists -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="entry"><?= $chunk[1] ?></span>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; <?php }?></div>
打开App,查看更多内容
随时随地看视频慕课网APP