使用关联数组创建表

时间:2016-02-17 11:13:05

标签: php arrays

我想使用关联键值对数组创建表。 我希望它的键作为表的列标题,值应该在相应的行单元格中。 数组可能看起来像

 Array
(
    [0] => Array
        (
            [field1] => 18270
            [field2] => 24
            [field3] => 7830
            [field4] => 44
            [field5] => 5
            [field6] => 15660
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [1] => Array
        (
            [field1] => 35070
            [field2] => 24
            [field3] => 15030
            [field4] => 44
            [field5] => 5
            [field6] => 30060
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [2] => Array
        (
            [field1] => 16051
            [field2] => 24
            [field3] => 6879
            [field4] => 44
            [field5] => 5
            [field6] => 13758
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [3] => Array
        (
            [field1] => 15050
            [field2] => 24
            [field3] => 6450
            [field4] => 44
            [field5] => 5
            [field6] => 12900
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [4] => Array
        (
            [field1] => 15750
            [field2] => 24
            [field3] => 6750
            [field4] => 44
            [field5] => 5
            [field6] => 13500
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [5] => Array
        (
            [field1] => 15750
            [field2] => 24
            [field3] => 6750
            [field4] => 44
            [field5] => 5
            [field6] => 13500
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [6] => Array
        (
            [field1] => 15050
            [field2] => 24
            [field3] => 6450
            [field4] => 44
            [field5] => 5
            [field6] => 12900
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )
)

和表应该如下table_image 我在动态创建thead并将相应的值放在它下面的列中时遇到了问题

2 个答案:

答案 0 :(得分:2)

试试这个:

<?php 
foreach ($table as $rowIndex=>$row) {
  if($rowIndex == 0) {
     echo "<thead><tr>";
     foreach ($row as $columnName=>$cell) {
        if(strpos($columnName, "field") === true) {
           echo "<td>$columnName</td>";
        }
     }
     echo "</tr></thead>";
    }
    echo "<tr>";
    foreach ($row as $columnName=>$cell) {
        if(strpos($columnName, "field") === true) {
           echo "<td>$cell</td>";
        }
    }
    echo "</tr>";
}
?>

更多关于strpos

答案 1 :(得分:1)

虽然不漂亮,但以下情况应该如此:

<?php
echo '<table>';
foreach (array_name as $row => $columns) {
    if ($row == 0) { //if it's the first row in our dataset
        //first print the table header
        echo '<thead><tr>';
        foreach ($columns as $header => $value) {
            echo '<th>' . $header . '</th>';
        }
        echo '</tr><thead>';
    }
        //then print the data as usual
        echo '<tr>';
        foreach ($columns as $header => $value) {
            echo '<td>' . $value . '</td>';
        }
        echo '</tr>';
}
echo '</table>';
?>

PS:kamaldeep-singh-bhatia击败拳击手:)