【问题标题】:Create HTML Table from nested PHP array从嵌套的 PHP 数组创建 HTML 表
【发布时间】:2015-03-10 17:47:04
【问题描述】:

我需要一些帮助,我正在尝试创建一个动态 HTML 表格,该表格将显示来自嵌套数组的一些结果。

我的数组总是这样设置的:

Array
(
    [column_label] => Size No.
    [column_data] => Array
        (
            [0] => Array
                (
                    [column_cell] => #3
                )

            [1] => Array
                (
                    [column_cell] => #3.5
                )

            [2] => Array
                (
                    [column_cell] => #4
                )

            [3] => Array
                (
                    [column_cell] => #4.5
                )

        )

)

Array
(
    [column_label] => Approx. Diameter Inches
    [column_data] => Array
        (
            [0] => Array
                (
                    [column_cell] => 3/32"
                )

            [1] => Array
                (
                    [column_cell] => 7/64"
                )

            [2] => Array
                (
                    [column_cell] => 1/8"
                )

            [3] => Array
                (
                    [column_cell] => 9/64"
                )

        )

)

Array
(
    [column_label] => Approx. Diameter mm
    [column_data] => Array
        (
            [0] => Array
                (
                    [column_cell] => 2.38
                )

            [1] => Array
                (
                    [column_cell] => 2.78
                )

            [2] => Array
                (
                    [column_cell] => 3.18
                )

            [3] => Array
                (
                    [column_cell] => 3.57
                )

        )

)

Array
(
    [column_label] => Catalog No.
    [column_data] => Array
        (
            [0] => Array
                (
                    [column_cell] => 32030
                )

            [1] => Array
                (
                    [column_cell] => 32035
                )

            [2] => Array
                (
                    [column_cell] => 32040
                )

            [3] => Array
                (
                    [column_cell] => 32045
                )

        )

)

我需要遍历每个 [column_data] 数组并准备它们以在我的表格中显示为表格单元格。

例如,我的表中的一行应包含 column_data 数组中键为 0 的表单元格。我表中的下一行将包含 column_data 数组中键为 1 的表单元格,依此类推。

希望这是有道理的,这样我确保单元格将始终对应于 Column_label 表格标题并垂直显示在单元格中。

这是我失败的尝试(未完成):

<?php if ($product_table) { 
    //Setup empty arrays
    $products = array();
    $labels = array();
    $data = array();
    $column_cells = array();

    foreach($product_table as $product) { 
        //echo '<pre>';
        //print_r($product);
        //echo '</pre>';
        //echo '<th>' . $product['column_label']  . '</th>';
        $products[] = $product;
        $labels[] = $product['column_label'];
        $data[] = $product['column_data'];

            foreach ($product['column_data'] as $column_data) {
                $column_cells[] = $column_data;
            }
        } 

?>  

<table class="table">
    <tr>
        <?php foreach($labels as $label) {
            echo '<th>' . $label . '</th>';
        } ?>
    </tr>

    <?php foreach($products as $product) { ?>
    <tr>
        <?php 
            echo '<pre>';
            print_r($product);
            echo '</pre>';
        ?>
    </tr>
    <?php } ?>
</table>

<?php } ?>

【问题讨论】:

  • 您对如何设置初始阵列有任何控制吗?只是好奇,因为它是按数据列而不是关联数组中的行来组织的,这使得它比它需要的复杂一些,而且更难阅读。
  • 不是真的,如果我打印 products 变量而不是 product 那么每个产品都有编号,不确定这是否有帮助。简而言之,我只是将 column_label 作为表格标题,并将 column_data 中的信息垂直显示在相应的表格标题下方。我想过使用 array_merge 或使用 for 循环和计数。仍然不确定,并且花了太多时间试图弄清楚。

标签: php html arrays


【解决方案1】:

您需要遍历您的column_data,并使用数组键,根据它的键将column_cell 添加到行中。

$labels = array();
$rows = array();

foreach($product_table as $product) { 

    $labels[] = $product['column_label'];

    foreach ($product['column_data'] as $key => $column_data) {
        $rows[$key][] = $column_data['column_cell'];
    }

} ?>  

然后在你的表格中,你需要遍历每个行数组,并打印单元格数据

<table class="table">
    <tr>
        <?php foreach($labels as $label) {
            echo '<th>' . $label . '</th>';
        } ?>
    </tr>
    <?php foreach($rows as $row) { ?>
    <tr>
        <?php foreach($row as $cell) {
            echo '<td>' . $cell . '</td>';
        } ?>
    </tr>
    <?php } ?>
</table>

使用您的样本数据 -

$product_table = array(
    array('column_label' => 'Size No.',
          'column_data' => array(
                0 => array('column_cell' => '#3'),
                1 => array('column_cell' => '#3.5'),
                2 => array('column_cell' => '#4'),
                3 => array('column_cell' => '#4.5')  )      
        ),
    array('column_label' => 'Approx. Diameter Inches',
          'column_data' => array(
                0 => array('column_cell' => '3/32"'),
                1 => array('column_cell' => '7/64"'),
                2 => array('column_cell' => '1/8"'),
                3 => array('column_cell' => '9/64"'))
        ),
    array('column_label' => 'Approx. Diameter mm',
          'column_data' => array(
                0 => array('column_cell' => '2.38'),
                1 => array('column_cell' => '2.78'),
                2 => array('column_cell' => '3.18'),
                3 => array('column_cell' => '3.57') )

        ),
    array('column_label' => 'Catalog No.',
          'column_data' => array(
                0 => array('column_cell' => '32030'),
                1 => array('column_cell' => '32035'),
                2 => array('column_cell' => '32040'),
                3 => array('column_cell' => '32045')  )

        )

);

你最终得到这个结果

【讨论】:

  • 你先生,是个圣人。
  • 总是乐于助人,祝您编码愉快。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 2012-11-23
  • 1970-01-01
相关资源
最近更新 更多