PHP 中每三次迭代

发布于 2024-09-30 05:09:49 字数 925 浏览 1 评论 0原文

我想在 PHP 循环的第三次迭代中输出一些特定的 HTML。这是我的代码:

<?php foreach ($imgArray as $row): ?>
   <div class="img_grid"><?= $row ?></div>
<?php endforeach; ?>

在这个循环的第三次迭代中,而不是显示:

<div class="img_grid"><?= $row ?></div>

我想显示:

<div class="img_grid_3"><?= $row ?></div>

如果我的数组循环 8 次,我想以这个结束:

   <div class="img_grid">[some html]</div>
   <div class="img_grid">[some html]</div>
   <div class="img_grid_3">[some html]</div>
   <div class="img_grid">[some html]</div>
   <div class="img_grid">[some html]</div>
   <div class="img_grid_3">[some html]</div>
   <div class="img_grid">[some html]</div>
   <div class="img_grid">[some html]</div>

谢谢

I would like to output some specific HTML on the third iteration of a loop in PHP. Here is my code:

<?php foreach ($imgArray as $row): ?>
   <div class="img_grid"><?= $row ?></div>
<?php endforeach; ?>

On the third iteration of this loop, Instead of displaying:

<div class="img_grid"><?= $row ?></div>

I would like to display:

<div class="img_grid_3"><?= $row ?></div>

I would like to end up with this if my array looped 8 times:

   <div class="img_grid">[some html]</div>
   <div class="img_grid">[some html]</div>
   <div class="img_grid_3">[some html]</div>
   <div class="img_grid">[some html]</div>
   <div class="img_grid">[some html]</div>
   <div class="img_grid_3">[some html]</div>
   <div class="img_grid">[some html]</div>
   <div class="img_grid">[some html]</div>

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2024-10-07 05:09:54

假设 $imgArray 是一个数组而不是关联数组(即它具有数字索引),这就是您想要的:

<?php foreach($imgArray as $idx => $row): ?>
  <?php if($idx % 3 == 2): ?>
    <div class="img_grid_3"><?php echo $row; ?></div>
  <?php else: ?>
    <div class="img_grid"><?php echo $row; ?></div>
  <?php endif; ?>
<?php endforeach; ?>

您可以像这样收紧它:

<?php foreach($imgArray as $idx => $row):
        if($idx % 3 == 2) {
          $css_class = 'img_grid_3';
        } else {
          $css_class = 'img_grid';
        }
?>
  <div class="<?php echo $css_class; ?>"><?php echo $row; ?></div>
<?php endforeach; ?>

或者甚至更多(有些人会只是在 HTML 中使用三元条件内联),但收益递减法则最终会在可读性方面发挥作用。不过,希望这能给您带来正确的想法。

Assuming $imgArray is an array and not an associative array (i.e. it has numeric indices), this is what you want:

<?php foreach($imgArray as $idx => $row): ?>
  <?php if($idx % 3 == 2): ?>
    <div class="img_grid_3"><?php echo $row; ?></div>
  <?php else: ?>
    <div class="img_grid"><?php echo $row; ?></div>
  <?php endif; ?>
<?php endforeach; ?>

You could tighten it up a bit like this:

<?php foreach($imgArray as $idx => $row):
        if($idx % 3 == 2) {
          $css_class = 'img_grid_3';
        } else {
          $css_class = 'img_grid';
        }
?>
  <div class="<?php echo $css_class; ?>"><?php echo $row; ?></div>
<?php endforeach; ?>

Or even more (some folks would just go with a ternary conditional inline in the HTML), but the law of diminishing returns kicks in eventually with regard to readability. Hopefully this gives you the right idea, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文