通过在php中的索引选择性地通过多维关联数组进行插值

我正在使用php开发网站,并且正在从sql服务器中获取数据。我需要做的是有2行,每行将包含3列数据。我存储数据的关联数组有6个子数组。我需要创建一个循环,其中在3个条目之后创建一个新行,并在下一行中打印下三个的数据。


我尝试使用while循环,但是我不知道如何通过它们的索引来分隔数组,而while($ index = mysqli_fetch_assoc(r))会打印所有数据,但我希望3出现在一行中,而3出现在下一行。谢谢!


        <?php

        $q = "SELECT * FROM main_intro";

        $r = mysqli_query($link, $q) or die(mysqli_error($link));

        $num_rows = mysqli_num_rows($r);

        $count = 0;

        $intro = mysqli_fetch_assoc($r);

        $keys = array_keys($intro) ?>


        <div class="row custom_pad">

            <?php

            for ($i = 0; $i < 3; $i++) {

                ?>

                <div class="col-md-4">

                    <h2><?php echo $intro['image']; ?><?php echo $intro['heading']; ?></h2>

                    <p><?php echo $intro['data']; ?></p>

                </div>

            <?php} ?>

            <!-- -->

        </div>

我需要的是,在第一行中,我需要前3个数组数据,然后在下一行中,我需要其他3个数组数据。


aluckdog
浏览 142回答 2
2回答

qq_遁去的一_1

看起来您想用适当的数据填充每行的三列。现在,您只从结果集中获取一行,您必须使用while循环遍历结果集,并使用该 for循环适当地填充每一列。<?php$q = "SELECT * FROM main_intro";$r = mysqli_query($link, $q) or die(mysqli_error($link));while($intro = mysqli_fetch_assoc($r)){&nbsp; &nbsp; ?>&nbsp; &nbsp; <div class="row custom_pad">&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; for ($i = 0; $i < 3; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-4">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch($i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $intro['image'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<h2>' . $intro['heading'] . '</h2>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<p>' . $intro['data'] . '</p>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; ?>&nbsp; &nbsp; </div>&nbsp; &nbsp; <?php}

慕容3067478

您可以在循环中应用条件并检查$iterator%3==0,如果条件匹配则增加数组索引例如$arr = [];$j=0;for($i=1;$i<=9;$i++){&nbsp; $arr[$j][] = $i;&nbsp; if($i%3==0) $j++;&nbsp;&nbsp;}echo '<pre>';print_r($arr);结果Array([0] => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [0] => 1&nbsp; &nbsp; &nbsp; &nbsp; [1] => 2&nbsp; &nbsp; &nbsp; &nbsp; [2] => 3&nbsp; &nbsp; )[1] => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [0] => 4&nbsp; &nbsp; &nbsp; &nbsp; [1] => 5&nbsp; &nbsp; &nbsp; &nbsp; [2] => 6&nbsp; &nbsp; )[2] => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [0] => 7&nbsp; &nbsp; &nbsp; &nbsp; [1] => 8&nbsp; &nbsp; &nbsp; &nbsp; [2] => 9&nbsp; &nbsp; )&nbsp; )
打开App,查看更多内容
随时随地看视频慕课网APP