php中嵌套for()循环的正确语法是什么,并且具有不同的增量?

php中嵌套for()循环的正确语法是什么,并且具有不同的增量?,php,for-loop,syntax,increment,nested-loops,Php,For Loop,Syntax,Increment,Nested Loops,我试图构建一个循环结构,在这个结构中,我知道循环的最大值,并且某些字段/输入有单独的增量增加。考虑到这一点,我认为应该采用嵌套的for()循环结构 作为一个例子,假设回响的目标可能看起来像这样: <table> <tr> <td><input id="1" secondaryId="1" ></td> <td><input id="2" secondaryId="1" >&

我试图构建一个循环结构,在这个结构中,我知道循环的最大值,并且某些字段/输入有单独的增量增加。考虑到这一点,我认为应该采用嵌套的for()循环结构

作为一个例子,假设回响的目标可能看起来像这样:

<table>
    <tr>
        <td><input id="1" secondaryId="1" ></td>
        <td><input id="2" secondaryId="1" ></td>
        <td><input id="3" secondaryId="1" ></td>
    </tr>
    <tr>
        <td><input id="4" secondaryId="2" ></td>
        <td><input id="5" secondaryId="2" ></td>
        <td><input id="6" secondaryId="2" ></td>
    </tr>
    <tr>
        <td><input id="7" secondaryId="3" ></td>
        <td><input id="8" secondaryId="3" ></td>
        <td><input id="9" secondaryId="3" ></td>
    </tr>
    <!-- etc -->
</table>
<table>
    <tr>
    <?php
        for ($i=1; $i <= $max; $i++) {
            printf('<td><input id="%d" secondaryId="%d" ></td>', $i, ($i/3)+1);
            if ($i % 3 == 0) { // boundary reached
                echo "</tr><tr>";
        }
    </tr>
</table>

对于“回声目标”,仅使用1表示循环就足够了。“id”属性将包含迭代变量
$i
,而“secondaryId”属性将包含
($i div 3)+1

for ($i=1; $i <= $max; $i++) {
    printf('<td><input id="%d" secondaryId="%d" ></td>', $i, ($i/3)+1);
}

对于($i=1;$i我认为您只需要使用其他变量,这些变量将根据您的需要对两个循环进行计数

$m = 1;
$l = 1;
echo"<table>";
for ($t=1;$t<4;$t++){
    echo"<tr>";
        for($y=1;$y<4;$y++){
            echo"<td><input id='$m' secondaryId='$l'></td>";
            $m++; //we increment here for ipunt id
        }
            $l++; //we increment here for secondaryId
    echo"</tr>";
}
echo"</table>";
$m=1;
$l=1;
回声“;
对于($t=1;$t)
你应该试试这个:-


您不需要重置第一个输入id

echo"<table>";
$inputId = 1;
for ($t=1;$t<4;$t++){
    echo"<tr>";
    for($y=1;$y<4;$y++){
        echo"<td><input id='$inputId' secondaryId='$y'></td>";
        $inputId++;
    }
    echo"</tr>";
}
echo"</table>";
echo”“;
$inputId=1;

对于($t=1;$t感谢这一点,这确实完美无瑕。此外,使用一个受Maiden B提出的解决方案启发的数学解决方案。下面,我还能够构造一个实现相同目的的单循环。谢谢!(我更新了我的问题,以反映我的想法)@Maiden B.感谢您的见解和建议。我必须承认,我不能完全理解您的脚本或语法,考虑到我的教育水平,这并不奇怪,但使用您将数学解决方案集成到单个循环中的想法,我能够在一个循环中获得所需的结果。我已更新我的问题以反映这不是我想到的。我希望我能在这里得到两个答案。
$m = 1;
$l = 1;
echo"<table>";
for ($t=1;$t<4;$t++){
    echo"<tr>";
        for($y=1;$y<4;$y++){
            echo"<td><input id='$m' secondaryId='$l'></td>";
            $m++; //we increment here for ipunt id
        }
            $l++; //we increment here for secondaryId
    echo"</tr>";
}
echo"</table>";
<table>
  <tr>
    <td><input id='1' secondaryId='1'></td>
    <td><input id='2' secondaryId='1'></td>
    <td><input id='3' secondaryId='1'></td>
  </tr>
  <tr>
    <td><input id='4' secondaryId='2'></td>
    <td><input id='5' secondaryId='2'></td>
    <td><input id='6' secondaryId='2'></td>
  </tr>
  <tr>
    <td><input id='7' secondaryId='3'></td>
    <td><input id='8' secondaryId='3'></td>
    <td><input id='9' secondaryId='3'></td>
  </tr>
</table>
<?php
echo"<table>";
$ids = 1;
for ($t=1;$t<4;$t++)
{
    echo"<tr>";
    for($y=1;$y<4;$y++)
    {
        echo"<td><input id='$ids' secondaryId='$t'></td>";
        $ids++;
    }
    echo"</tr>";
}
echo"</table>";
?>
echo"<table>";
$inputId = 1;
for ($t=1;$t<4;$t++){
    echo"<tr>";
    for($y=1;$y<4;$y++){
        echo"<td><input id='$inputId' secondaryId='$y'></td>";
        $inputId++;
    }
    echo"</tr>";
}
echo"</table>";