表行计数混乱PHP Foreach数组

时间:2012-11-24 09:17:06

标签: php arrays multidimensional-array foreach html-table

我有这样的数组

Array  ([0] => 8 [1] => 8 [2] => 1 [3]=> 7 [4] => 8 )  

我在PHP上使用ForEach在上面的数组中形成了表行。但是当你看上面的数组时,将在第3个ROW上创建7个TD,在第4个ROW上创建8个TD。我需要在行的第三行移动那个空白的TD,而不是在行的末尾显示它。是否可能?

<table border="1"><tbody>
  <tr align="right">
    <td align="right" colspan="0" rowspan="0" id="td_02">02</td>
    <td align="right" colspan="0" rowspan="0" id="td_06">06</td>
    <td align="right" colspan="0" rowspan="0" id="td_10">10</td>
    <td align="right" colspan="0" rowspan="0" id="td_14">14</td>
    <td align="right" colspan="0" rowspan="0" id="td_18">18</td>
    <td align="right" colspan="0" rowspan="0" id="td_22">22</td>
    <td align="right" colspan="0" rowspan="0" id="td_26">26</td>
    <td align="right" colspan="0" rowspan="0" id="td_30">30</td>
  </tr>
  <tr align="right">
    <td align="right" colspan="0" rowspan="0" id="td_01">01</td>
    <td align="right" colspan="0" rowspan="0" id="td_05">05</td>
    <td align="right" colspan="0" rowspan="0" id="td_09">09</td>
    <td align="right" colspan="0" rowspan="0" id="td_13">13</td>
    <td align="right" colspan="0" rowspan="0" id="td_17">17</td>
    <td align="right" colspan="0" rowspan="0" id="td_21">21</td>
    <td align="right" colspan="0" rowspan="0" id="td_25">25</td>
    <td align="right" colspan="0" rowspan="0" id="td_29">29</td>
  </tr>
  <tr align="right">
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
    <td>XX</td>
  </tr>
  <tr align="right">
    <td align="right" colspan="0" rowspan="0" id="td_03">03</td>
    <td align="right" colspan="0" rowspan="0" id="td_07">07</td>
    <td align="right" colspan="0" rowspan="0" id="td_11">11</td>
    <td align="right" colspan="0" rowspan="0" id="td_15">15</td>
    <td align="right" colspan="0" rowspan="0" id="td_19">19</td>
    <td align="right" colspan="0" rowspan="0" id="td_23">23</td>
    <td align="right" colspan="0" rowspan="0" id="td_27">27</td>
  </tr>
  <tr align="right">
    <td align="right" colspan="0" rowspan="0" id="td_31">31</td>
    <td align="right" colspan="0" rowspan="0" id="td_04">04</td>
    <td align="right" colspan="0" rowspan="0" id="td_08">08</td>
    <td align="right" colspan="0" rowspan="0" id="td_12">12</td>
    <td align="right" colspan="0" rowspan="0" id="td_16">16</td>
    <td align="right" colspan="0" rowspan="0" id="td_20">20</td>
    <td align="right" colspan="0" rowspan="0" id="td_24">24</td>
    <td align="right" colspan="0" rowspan="0" id="td_28">28</td>
  </tr>
</tbody></table>

1 个答案:

答案 0 :(得分:1)

我完全不知道你是如何制作你的桌子的,但是......

避免“空白”td问题的一种方法是确保所有行都具有相同数量的td,这可以通过查找数组的max值来完成。

例如:

<?php
    $row_array = array(8, 8, 1, 7, 8);
    $num_tds = max($row_array);
?>
<table border="1" cellpadding="3" cellspacing="3">
    <?php foreach($row_array as $row) : ?>
    <tr>
        <?php for ($i = 0; $i < $num_tds; $i++) : ?>
        <td>&nbsp;</td>
        <?php endfor; ?>
    </tr>
    <?php endforeach; ?>
</table>

如果没有真正看到你的代码,我只是提供盲目解决方案,但我希望它有所帮助。