如何以表格形式显示PHP3级别数组

如何以表格形式显示PHP3级别数组,php,Php,目前我有一个PHP3级别的数组。如何以表格形式显示它?使用print\r,我可以显示完整的数组,但我需要美化它以表格形式显示。可能吗 要插入的数组示例如另一篇帖子所示:所以。。。数组的每一级都应该是一个嵌入式表 <table> <?php // FIRST LEVEL foreach ($myArray as $first_level): ?> <tr> <td>The header of the first

目前我有一个PHP3级别的数组。如何以表格形式显示它?使用print\r,我可以显示完整的数组,但我需要美化它以表格形式显示。可能吗


要插入的数组示例如另一篇帖子所示:

所以。。。数组的每一级都应该是一个嵌入式表

<table>
    <?php // FIRST LEVEL
    foreach ($myArray as $first_level): ?>
    <tr>
        <td>The header of the first level, here's some data <?php echo $first_level['some_data']; ?></td>
    </tr>
    <tr>
        <td>
            <table>
            <?php // SECOND LEVEL
                foreach($first_level['second_level'] as $second_level): ?>
                    <tr>
                        <td><?php echo $second_level['some_data']; ?></td>
                    </tr>
            <?php endforeach; ?>
                </table>
        </td>
    </tr>
    <?php endforeach; ?>
</table>

..并不断重复该模式

有很多方法可以做到这一点,因为您没有为输出格式提供模板,所以更是如此。。。。 假设输入数组$src的每个元素$e代表表中的一行。 那么$e[0]是字符串元素1,2,3,$e[1]是对应的数组1,2,3,4,5,6或7,8,9。 让我们把$e[0]放入。。。元素

foreach( $src as $e ) {
  echo '<th>', $e[0], '</th>';
}
然后将每个元素用$e[1]包装在

现在把它包装成另一个。。。你就完了

foreach( $src as $e ) {
  echo '<tr>';
  echo '<th>', $e[0], '</th>';
  foreach($e[1] as $v) {
    echo '<td>', $v, '</td>';
  }
  echo "</tr>\r\n";
}
同样的事情稍微短一点看

输出是

<tr><th>one</th><td>1</td><td>2</td><td>3</td></tr>
<tr><th>two</th><td>4</td><td>5</td><td>6</td></tr>
<tr><th>three</th><td>7</td><td>8</td><td>9</td></tr>

另请参见:

这是一个非常简单的解决方案。到目前为止你试过什么?
<?php
$src = getData();
foreach( $src as $e ) {
    echo '<tr><th>', $e[0], '</th><td>', join('</td><td>', $e[1]), "</td></tr>\n";
}

function getData() {
    return array(
        array( 'one', array(1,2,3) ),
        array( 'two', array(4,5,6) ),
        array( 'three', array(7,8,9) )
    );
}
<tr><th>one</th><td>1</td><td>2</td><td>3</td></tr>
<tr><th>two</th><td>4</td><td>5</td><td>6</td></tr>
<tr><th>three</th><td>7</td><td>8</td><td>9</td></tr>