PHP:如果数字可以被 3 整除,减 1(2、5、8、11 等)

PHP: If number is divisble by 3, minus 1 (2, 5, 8, 11 etc)

我有一个循环,它使用数据库中的一些内容创建 divs。我有一个变量 $current_count,我从值“0”开始,这是我循环的第一次迭代。

我正在使用:

if ($current_count == 0 || $current_count % 3 == 0) { echo '<div class="parent">'; }

在循环的最顶端创建一个父级 div,然后在每次迭代中再次 divisible by 3。它看起来像这样(数字代表迭代):

0 <div class="parent">
0    <div class="child"></div>
1    <div class="child"></div>
2    <div class="child"></div>
3 <div class="parent">
3    <div class="child"></div>
4    <div class="child"></div>
5    <div class="child"></div>

但问题是我不知道如何关闭那些 div,因为它们会在不同的迭代中关闭。例如,在迭代 0 时打开的父项需要在迭代 2 结束时关闭。

我需要基本上说(伪代码):

IF $current_count is equal to (division of 3, minus 1) { etc }

我试过:

if ($current_count % 3 == (0 - 1)) {}
if ($current_count % (3 == 0) - 1) {}
if ($current_count % 3 == 0 - 1) {}

但是其中 none 个返回真值。有谁知道我可以这样做的方法吗?

干杯, 李.

更新 1:这是当前 PHP 代码的示例,以更好地解释我要完成的工作:

$current_count = '0';
$ret = '';

        foreach ( $brands as $index => $brand ) : 

if ($current_count == 0 || $current_count % 3 == 0) {
                    $ret.= '<div class="parent">'; //Start parent
                }

                $ret.= '<div class="child"></div>'; //Child

if ($current_count % 3 == (0 - 1)) { // IF LINE 2, 5, 8, 11 etc, NOT WORKING
                            $ret.= '</div>'; // End the parent
                        }

            $current_count++;
        endforeach;

试试这个,

    for($i = 0; $i <= 10; $i++) {
         if($i % 3 == 0 && $i > 0)// $i > 0 condition because. 0 % 3 is equal to 0 only.
              echo $i - 1;// will echo 2,5,8
              echo "</div>";// in your case.
    }