【问题标题】:Why isn't this foreach loop, looping?为什么这个 foreach 循环不是循环的?
【发布时间】:2014-01-21 13:47:53
【问题描述】:

这个foreach循环应该循环12次,但只循环4次。怎么会?我究竟做错了什么?我正在尝试将产品图片放入滑动轮播中。前 4 张图片看起来没有问题,但其他 8 张根本不会出现。

<div class="container">
    <div class="col-md-12">
        <h1>In the Spotlight</h1>

        <div class="well">
            <div id="myCarousel" class="carousel slide">
                <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#myCarousel" data-slide-to="1"></li>
                    <li data-target="#myCarousel" data-slide-to="2"></li>
                </ol>
                <!-- Carousel items -->
                <div class="carousel-inner">


                    <?php 
                    foreach ($products as $key => $product) { ?>

                    <div class="item <?php echo ($key == 0?"active":"");?>">

                    <?php if ($key == 0) 
                    {
                        echo "<div class=\"row\">";
                    }
                    elseif ($key == 4) 
                    {
                        echo "<div class=\"row\">";
                    }
                    elseif ($key == 8) 
                    {
                        echo "<div class=\"row\">";
                    }
                    else
                    {

                    }
                    ?>

                    <?php if ($product['thumb']) { ?>
                    <div class="col-md-3"><a class="thumbnail" href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" style="max-width: 100%;" /></a></div>
                    <?php } ?>


                    <?php if ($key == 3) 
                    {
                        echo "</div></div>";
                    }
                    elseif ($key == 7) 
                    {
                        echo "</div></div>";
                    }
                    elseif ($key == 11) 
                    {
                        echo "</div></div>";
                    }
                    else
                    {

                    }
                    ?>


                  <?php } ?>



</div>
</div>
                <!--/carousel-inner--> <a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>

                <a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
            </div>
            <!--/myCarousel-->
        </div>
        <!--/well-->
    </div>
</div>

源输出

<div class="container">
    <div class="col-md-12">
        <h1>In the Spotlight</h1>

        <div class="well">
            <div id="myCarousel" class="carousel slide">
                <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#myCarousel" data-slide-to="1"></li>
                    <li data-target="#myCarousel" data-slide-to="2"></li>
                </ol>
                <!-- Carousel items -->
                <div class="carousel-inner">



                    <div class="item active">

                    <div class="row">
                                        <div class="col-md-3"><a class="thumbnail" href="http://localhost/theme/index.php?route=product/product&amp;product_id=43"><img src="http://localhost/theme/image/cache/data/demo/macbook_1-250x250.jpg" alt="MacBook" style="max-width: 100%;" /></a></div>





                    <div class="item ">


                                        <div class="col-md-3"><a class="thumbnail" href="http://localhost/theme/index.php?route=product/product&amp;product_id=40"><img src="http://localhost/theme/image/cache/data/demo/iphone_1-250x250.jpg" alt="iPhone" style="max-width: 100%;" /></a></div>





                    <div class="item ">


                                        <div class="col-md-3"><a class="thumbnail" href="http://localhost/theme/index.php?route=product/product&amp;product_id=42"><img src="http://localhost/theme/image/cache/data/demo/w1-250x250.jpg" alt="Apple Cinema 30&quot;" style="max-width: 100%;" /></a></div>





                    <div class="item ">


                                        <div class="col-md-3"><a class="thumbnail" href="http://localhost/theme/index.php?route=product/product&amp;product_id=49"><img src="http://localhost/theme/image/cache/data/demo/samsung_tab_1-250x250.jpg" alt="Samsung Galaxy Tab 10.1" style="max-width: 100%;" /></a></div>


                    </div></div>                    




</div>
</div>
                <!--/carousel-inner--> <a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>

                <a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
            </div>
            <!--/myCarousel-->
        </div>
        <!--/well-->
    </div>
</div>

print_r($products) 显示:

Array ( [0] => Array ( [product_id] => 43 [thumb] => http://localhost/theme/image/cache/data/demo/macbook_1-250x250.jpg [name] => MacBook [price] => £500.00 [special] => [rating] => 0 [reviews] => Based on 0 reviews. [href] => http://localhost/theme/index.php?route=product/product&product_id=43 ) [1] => Array ( [product_id] => 40 [thumb] => http://localhost/theme/image/cache/data/demo/iphone_1-250x250.jpg [name] => iPhone [price] => £101.00 [special] => [rating] => 0 [reviews] => Based on 0 reviews. [href] => http://localhost/theme/index.php?route=product/product&product_id=40 ) [2] => Array ( [product_id] => 42 [thumb] => http://localhost/theme/image/cache/data/demo/w1-250x250.jpg [name] => Apple Cinema 30" [price] => £100.00 [special] => £90.00 [rating] => 0 [reviews] => Based on 0 reviews. [href] => http://localhost/theme/index.php?route=product/product&product_id=42 ) [3] => Array ( [product_id] => 49 [thumb] => http://localhost/theme/image/cache/data/demo/samsung_tab_1-250x250.jpg [name] => Samsung Galaxy Tab 10.1 [price] => £199.99 [special] => [rating] => 0 [reviews] => Based on 0 reviews. [href] => http://localhost/theme/index.php?route=product/product&product_id=49 ) )

提前致谢

【问题讨论】:

  • print_r($products) 显示什么?
  • 在此处显示结果。我们无法在没有看到的情况下帮助您。
  • 你为什么要使用 $flag 变量? $key 变量不是已经是数字了吗?
  • 但您没有显示 print_r($products) 的输出
  • 罪魁祸首可能是$flag ...检查他

标签: php foreach


【解决方案1】:

foreach 总是循环遍历数组,因此:您的数组有 4 个项目,这就是 foreach 进行 4 次迭代的原因。在这种情况下,foreach 类似于:

for ($i = 0, $c = count($your_array); $i < $c; $i++)

也就是说,您不应该期望 foreach 的迭代次数超过数组中的项目数。

【讨论】:

  • 这是正确的,数组中只有 4 个项目。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2019-08-03
  • 2023-03-18
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
相关资源
最近更新 更多