简体   繁体   中英

php: output Array in html table

How can I write all values ​​into an HTML table?

This is the Array:

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
                )
        )

)

I can only output all values. Like this:

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

How can I access the values ​​$ table ['data'] [1] .. with a foreach? To write them into a table cell like this:

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

Your foreach loop will be as below :

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>";

As you have column headers embedded within the data, I'd print the table header first and then loop through the rest of the data.

<?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>

Output:

<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>
<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>

You can try this.

<?php
if(!empty($table['data'])){ 
    echo "<table><body>";
    foreach($table['data'] as $data){
        echo "<tr>";
        echo "<td>".$data[0]."</td>";
        echo "<td>".$data[1]."</td>";
        echo "<td>".$data[2]."</td>";
        echo "</tr>";
    }
    echo "</body></table>";
}

?>

Try this Code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM