php:在html表中输出数组

时间:2017-08-21 07:03:09

标签: php html arrays

如何将所有值都写入HTML表格?

这是数组:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [0] => 
                    [1] => sortprice
                    [2] => sortdate
                )

            [1] => Array
                (
                    [0] => a lorem ipsum
                    [1] => 1. Preis
                    [2] => 1998
                )

            [2] => Array
                (
                    [0] => b lorem ipsum
                    [1] => 2. Preis
                    [2] => 1997
                )

            [3] => Array
               (
                    [0] => c lorem ipsum
                    [1] => 3. Preis
                    [2] => 1996
                )
        )

)

我只能输出所有值。 像这样:

foreach ($table['data'] as $v1) {
        foreach ($v1 as $v2) {
            echo "$v2\n";
        }
    }

如何使用foreach访问$ table [' data'] [1] ..的值?将它们写入这样的表格单元格:

<table>
    <tr>
        <td>a lorem ipsum</td>
        <td>1. Preis</td>
        <td>1998</td>
    </tr>
    .....
</table>

4 个答案:

答案 0 :(得分:1)

你的foreach循环如下:

echo "<table border='1'>";
foreach ($table['data'] as $v1) { //loop for ROW
    echo "<tr>";
    foreach ($v1 as $v2) { //loop for CELL
        echo "<td>$v2</td>";
    }
    echo "</tr>";
}
echo "</table>";

答案 1 :(得分:1)

由于您在数据中嵌入了列标题,我首先打印表头,然后遍历其余数据。

<?php

$table = [
    'data' => [
        [
            'Column 1',
            'Column 2',
            'Column 3'
        ],
        [
            'foo',
            'bar',
            'baz'
        ],
        [
            'big',
            'fat',
            'mamma'
        ]
    ]
];

$data = $table['data'];
?>
<table>
    <thead>
        <tr>
            <th><?= $data[0][0] ?></th>
            <th><?= $data[0][1] ?></th>
            <th><?= $data[0][2] ?></th>
        </tr>
    </thead>
    <tbody>
    <?php
    array_shift($data);
    foreach($data as $value)
    {
    ?>
        <tr>
            <td><?= $value[0] ?></td>
            <td><?= $value[1] ?></td>
            <td><?= $value[2] ?></td>
        </tr>
    <?php
    }
    ?>
    </tbody>
</table>

输出:

<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
    </thead>
    <tbody>
            <tr>
            <td>foo</td>
            <td>bar</td>
            <td>baz</td>
        </tr>
            <tr>
            <td>big</td>
            <td>fat</td>
            <td>mamma</td>
        </tr>
        </tbody>
</table>

答案 2 :(得分:0)

<table>
    <tr><td>ID</td><td colspan="2"><?php echo $table['id']?></td></tr>
    <tr><td>Name</td><td colspan="2"><?php echo $table['nmae']?></td></tr>
    <tr><td>Description</td><td colspan="2"><?php echo $table['description']?></td></tr>
    <tr><td>Author</td><td colspan="2"><?php echo $table['author']?></td></tr>
    <tr><td>Last Modified</td><td colspan="2"><?php echo $table['last_modified']?></td></tr>
    <?php foreach($table['data'] as $v1){ ?>
    <tr><td><?php echo $v1[0];?></td><td><?php echo $v1[1];?></td><td><?php echo $v1[2];?></td></tr>
    <?php } ?>
</table>

你可以试试这个。

答案 3 :(得分:0)

{{1}}

试试此代码。