PHP - 每 4 次将 div 添加到 foreach 循环中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8753786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:25:49  来源:igfitidea点击:

PHP - Adding divs to a foreach loop every 4 times

php

提问by BigJobbies

I need a little help with a foreach loop.

我需要一个 foreach 循环的帮助。

Basically what i need to do is wrap a div around the output of the data every 4 loops.

基本上我需要做的是每 4 次循环在数据输出周围环绕一个 div。

I have the following loop:

我有以下循环:

foreach( $users_kicks as $kicks ) {
    echo $kicks->brand;
}

For every 4 times it echos that out i want to wrap it in a so at the end it will look like so:

每 4 次它都会回应我想把它包裹在一个所以最后它看起来像这样:

<div>
    kicks brand
    kicks brand
    kicks brand
    kicks brand
</div>
<div>
    kicks brand
    kicks brand
    kicks brand
    kicks brand
</div>
<div>
    kicks brand
    kicks brand
    kicks brand
    kicks brand
</div>

and so on.

等等。

Cheers

干杯

回答by Ninja

$count = 1;
foreach( $users_kicks as $kicks ) 
{
    if ($count%4 == 1)
    {  
         echo "<div>";
    }
    echo $kicks->brand;
    if ($count%4 == 0)
    {
        echo "</div>";
    }
    $count++;
}
if ($count%4 != 1) echo "</div>"; //This is to ensure there is no open div if the number of elements in user_kicks is not a multiple of 4

回答by Laurence

This answer is very late - but in case people see it - this is a cleaner solution, no messy counters and ifstatements:

这个答案很晚了 - 但如果人们看到它 - 这是一个更干净的解决方案,没有凌乱的计数器和if语句:

foreach (array_chunk($users_kicks, 4, true) as $array) {
    echo '<div>';
    foreach($array as $kicks) {
         echo $kicks->brand;
    }
    echo '</div>';
}

You can read about array_chunk on php.net

您可以在 php.net 上阅读有关array_chunk 的信息

回答by adatapost

Try % modulus operator.

试试 % 模运算符。

$i=1;
//div begins
foreach( $users_kicks as $kicks ) {
   if($i % 4 ==0)
    {
      //div ends
      //div begins
    }
    echo $kicks->brand;
   $i++;
}
//div ends

回答by dwaskowski

you can also use array_chunkwhich cut array by blocks

您还可以使用按块切割数组的array_chunk

$blocks = array_chunk($users_kicks, 4);
foreach ($blocks as $block) {
    echo '<div>';
    foreach ($block as $kicks) {
        echo $kicks->brand;
    }
    echo '</div>';
}

回答by Chandan Sharma

<?php
$item_count=1;
$items_block=3;
?>
<div class="wrapper">
    <?php if(!empty($list)){ ?>

        <div class="item_block">
            <?php foreach ($list as $val){ ?>
                <div>Item</div>

                <?php 
                if($item_count % $items_block==0){ ?>
                    </div>
                    <div class="item_block">
                <?php 
                } 
                $item_count++;
                ?>

            <?php endforeach; ?>
        </div>
    <?php } ?>
</div>

回答by Ferdinand Liu

A little modification to AVD's answer to make sure there is no empty DIV if array is empty or it's count is factor of 4...

对 AVD 的答案稍作修改,以确保如果数组为空或它的计数为 4 的因子,则没有空的 DIV...

if($lastRec=count($user_kicks)){
  echo '<div>';
  $i=1;
  foreach( $users_kicks as $kicks ) {
    if( ($i % 4 == 0) && ($i<$lastRec) ) echo '</div><div>';
    echo $kicks->brand;
    $i++;
  }
  echo '</div>';
}