php foreach循环帮助

问题描述

| 我有一个foreach循环,该循环基本上为我构建了一个产品页面,将我的产品排成3行。 看代码
foreach ($product_sets as $product)
{
    $currentRow = ceil($currentItem / 3);
    $currentColumn = $currentItem - (($currentRow - 1) * 3);
    if ($number_of_blanks == 2) :
        if (($number_of_rows > 1 && $currentRow == ($number_of_rows - 1) && $currentColumn == 2) || ($number_of_rows == 1 && $currentColumn == 1)) :
    ?>
            <li><img src=\"<?PHP echo site_url(\'assets/img/blocks/guarantee.png\'); ?>\" alt=\"5 Year Guarantee\" width=\"242\" height=\"156\"></li>
    <?PHP
            $currentItem++;
        endif;
    endif;
    ?>
    <li class=\"<?PHP if($currentItem % 3 == 0) echo \'endHomeBlock\';?>\">
        <?PHP $this->load->view(\'blocks/product_small\',array(\'product\' => $product)); ?>
    </li>
    <?PHP
        $currentItem++;

    }
我想要做的是在第一行的末尾放置一个图像(一个销售点),并在其他各行中随机放置一个图像,但是在一行中保留3个项目(包括销售点的图像)。我将图像路径放在名为images的数组中,看起来与此类似,
$images = array(
  \'iamge1.png\',\'image2.png,\'image3.png,\'image4.png,);
我该如何实现?我到处乱跑了几个小时:(     

解决方法

抱歉,我没有太多时间为您编写完整的代码,但是以下方法应该可以工作:
<ul>
    <li><?php
        foreach($items as $i=>$item){
            // ...write item...
            if(($i % 3)==0 && $i!=0){ // if multiple of 3 and not the first time..
                ?></li><li><?php
            }
        }
    ?></li>
</ul>
    ,因此,我要做的是创建图像的哈希表,并在表中的什么位置显示它们。该表的键将是索引,而值将是图像名称。 假设“ 3”是从零开始的,那么第一行中的第三项的第一个键就是“ 4”。 然后在循环中,检查哈希表中是否有“ 3”。如果是,打印图像并增加increment3ѭ(并重新计算行和列),然后打印
$product
。如果它不在哈希表中,则像平常一样打印
$product
。