【问题标题】:PHP: Troubles on indexing a nested for-loopPHP:索引嵌套for循环的麻烦
【发布时间】:2015-02-03 01:05:22
【问题描述】:

我正在尝试将 3 维数组打印到表格中。但指数有点搞砸了。当我使用以下(伪)代码时:

...
<<print headers and stuff>>

for ( $i = 0; $i < count( $array ); i++) {
    $itemArray = $array[i];

    for ( $j = 0; $j < count( $itemArray; j++) {
        $innerItem = $itemArray[j];
        echo <<tr start + both indexes in td>>

        foreach ($innerItem as $spec) {
            echo <<td with item>>
        }

        echo <<tr stop>>
    }
}

在本例中,我使用 i 作为外部数组的索引,使用 j 作为内部数组的索引(非常明显)。 我从中得到的结果如下:

| index i | index j | title1 | title2 |
|    0    |    0    |        |        |
|    1    |    0    |        |        |
|    2    |    0    |        |        |
|   ...   |   ...   |        |        |

虽然我希望:

| index i | index j | title1 | title2 |
|    0    |    0    |        |        |
|    0    |    1    |        |        |
|    1    |    0    |        |        |
|    1    |    1    |        |        |
|    1    |    2    |        |        |
|    2    |    0    |        |        |
|   ...   |   ...   |        |        |

(原始)完整代码是:

echo "<h1>Combat analysis</h1>";
echo '<table cellspacing="0" cellpadding="4" border="1"><tbody>';
echo "<tr><td>#Mon</td><td>#Att</td><td>DungLVL</td><td>CharLVL</td><td>Health</td><td>Weapon</td><td>No. potions</td></tr>";
for ($battleIndex  = 0; $battleIndex < count($this->combatLog); $battleIndex++) {
    $battle = $this->combatLog[$battleIndex];
    for ($attackIndex = 0; $attackIndex < sizeof($battle); $attackIndex++) {
        $attack = $battle[$attackIndex];
        echo "<tr><td>" . $battleIndex . "</td><td>" . $attackIndex . "</td>";
        foreach ($attack as $stat) {
            echo "<td>" . $stat . "</td>";
        }
        echo "</tr>";
    }
}
echo "</tbody></table>";

出了什么问题?

【问题讨论】:

  • 这只是意味着你所有的战斗都有大小 1。很可能错误在于你填充它们的方式,而不是在这段代码中。

标签: php for-loop indexing


【解决方案1】:

测试您的代码并按预期运行。您应该执行echo '&lt;pre&gt;'.print_r($this-&gt;combatLog).'&lt;/pre&gt;'; 并调试数组内容。

另外,我会向您推荐以下内容:

1) 可以用foreach代替for,例如:foreach ($this-&gt;combatLog as $battleIndex =&gt; $battle)

2) 如果您不确定数组是否包含值,您应该首先执行以下操作:if (is_array($this-&gt;combatLog) &amp;&amp; count($this-&gt;combatLog) &gt; 0)

3) 为简单起见和代码维护,我将首先循环多维数组并将其转换为称为 $attacks 的一维,其中包含每个攻击的数组,该数组由您可以识别的键索引,ej:

$attacks=array();
$attacks[]=array(
'Mon'=>$battleIndex,
'Att'=>$attackIndex,
'DungLVL'=>isset($stat[0])?$stat[0]:null,
'CharLVL'=>isset($stat[1])?$stat[1]:null,
'Health'=>isset($stat[2])?$stat[2]:null,
'Weapon'=>isset($stat[3])?$stat[3]:null,
'Potions'=>isset($stat[4])?$stat[4]:null,
);

然后你可以定义一些列,例如:

$columns=array(
    'Mon',
    'Att',
    'DungLVL',
    'CharLVL',
    'Health',
    'Weapon',
    'Potions',
);

然后像这样打印表头:

echo '<tr>';
foreach ($columns as $column) {
  echo '<td>'.$column.'</td>';
}
echo '</tr>';

并像这样打印行:

foreach ($attacks as $attack) {
  echo '<tr>';
  foreach ($columns as $column) {
    echo '<td>'.$attack[$column].'</td>';
  }
  echo '</tr>';
}

【讨论】:

    猜你喜欢
    • 2020-05-13
    • 2017-05-03
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多