在HTML表中打印PHP数组

时间:2015-12-15 14:40:15

标签: php html arrays

使用Array中的数据创建我的html表时遇到了一些麻烦。它应该是一个简单的价格表,每种数量*格式的其他价格。

这是我的阵列:

Array
(
    [item] => Array
        (
            [id] => 1
            [name] => item_name
            [description] => description
            [category_id] => 2
        )

    [format] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [item_id] => 1
                    [title] => 25x140mm
                )

            [2] => Array
                (
                    [id] => 2
                    [item_id] => 1
                    [title] => 50x215mm
                )

            [3] => Array
                (
                    [id] => 3
                    [item_id] => 1
                    [title] => 25x100mm
                )

            [4] => Array
                (
                    [id] => 4
                    [item_id] => 1
                    [title] => 25x150mm
                )

        )

    [quantity] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [item_id] => 1
                    [quantity] => 100
                )

            [2] => Array
                (
                    [id] => 2
                    [item_id] => 1
                    [quantity] => 250
                )

            [3] => Array
                (
                    [id] => 3
                    [item_id] => 1
                    [quantity] => 500
                )

            [4] => Array
                (
                    [id] => 4
                    [item_id] => 1
                    [quantity] => 1000
                )

        )

    [prices] => Array
        (
            [1] => Array
                (
                    [1] => Array
                        (
                            [format] => 25x140mm
                            [quantity] => 100
                            [price] => 111.00
                        )

                    [4] => Array
                        (
                            [format] => 25x140mm
                            [quantity] => 1000
                            [price] => 114.00
                        )

                    [3] => Array
                        (
                            [format] => 25x140mm
                            [quantity] => 500
                            [price] => 113.00
                        )

                    [2] => Array
                        (
                            [format] => 25x140mm
                            [quantity] => 250
                            [price] => 112.00
                        )

                )

            [3] => Array
                (
                    [4] => Array
                        (
                            [format] => 25x100mm
                            [quantity] => 1000
                            [price] => 134.00
                        )

                    [3] => Array
                        (
                            [format] => 25x100mm
                            [quantity] => 500
                            [price] => 133.00
                        )

                    [2] => Array
                        (
                            [format] => 25x100mm
                            [quantity] => 250
                            [price] => 132.00
                        )

                    [1] => Array
                        (
                            [format] => 25x100mm
                            [quantity] => 100
                            [price] => 131.00
                        )

                )

            [2] => Array
                (
                    [4] => Array
                        (
                            [format] => 50x215mm
                            [quantity] => 1000
                            [price] => 124.00
                        )

                    [3] => Array
                        (
                            [format] => 50x215mm
                            [quantity] => 500
                            [price] => 123.00
                        )

                    [2] => Array
                        (
                            [format] => 50x215mm
                            [quantity] => 250
                            [price] => 122.00
                        )

                    [1] => Array
                        (
                            [format] => 50x215mm
                            [quantity] => 100
                            [price] => 121.00
                        )

                )

            [4] => Array
                (
                    [3] => Array
                        (
                            [format] => 25x150mm
                            [quantity] => 500
                            [price] => 143.00
                        )

                    [2] => Array
                        (
                            [format] => 25x150mm
                            [quantity] => 250
                            [price] => 142.00
                        )

                    [1] => Array
                        (
                            [format] => 25x150mm
                            [quantity] => 100
                            [price] => 141.00
                        )

                    [4] => Array
                        (
                            [format] => 25x150mm
                            [quantity] => 1000
                            [price] => 144.00
                        )

                )

        )  
)

我从$item['format']获取表格的标题,从$item['quantity']获取数量。

这是我试图创建的html表:

<table id="table">
        <thead>
          <tr>
            <th>Anzahl \ Format</th>
            <?php foreach ($item['format'] as $format) {  ?>
            <th><?php print_r($format['title']) ?></th>
            <?php } ?>
          </tr>
        </thead>

        <tbody>
          <?php foreach ($item['quantity'] as $quantity) {
            echo "<tr>";
            echo "<td>";
            print_r($quantity['quantity']);
            echo "</td>";
              foreach ($item['prices'] as $key => $value) {
                echo "<td>";
                print_r($value);
                echo "</td>";
              }
            echo "</tr>";
          } ?>
        </tbody>
</table>

以下是我的表格的样子: enter image description here

以下是现在的样子: enter image description here

我的问题是为每种数量\格式获得实际价格。

1 个答案:

答案 0 :(得分:1)

修复语法错误后,您可以尝试这样的事情:

<?php
foreach ($item['quantity'] as $quantity) {
    echo "<tr>";
    echo "<td>";
    print_r($quantity['quantity']);
    echo "</td>";
    foreach ($item['prices'] as $key => $value) {
        echo "<td>";
        // since we know a quantity for this row,
        // check each element in this price array for equality 
        foreach ($value as $price) {
            if ($price['quantity'] == $quantity['quantity']) {
                echo $price['price'];
                break; // we will not continue further
            }
        }
        print_r($value);
        echo "</td>";
    }
    echo "</tr>";
}
?>