如何在以下场景中以表格格式在smarty中显示数组数据?

时间:2014-04-24 13:23:37

标签: php arrays foreach smarty

我有一个标题为$preview_data的数组如下:

Array
(
    [op] => preview
    [id] => 
    [form_submitted] => yes
    [company_id] => 
    [product_id] => 
    [pack] => Array
        (
            [0] => 10
            [1] => 20
            [2] => 30
            [3] => 40
        )

    [quantity] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 6
            [3] => 8
        )

    [volume] => Array
        (
            [0] => 100
            [1] => 200
            [2] => 300
            [3] => 400
        )

    [units] => Array
        (
            [0] => 5
            [1] => 7
            [2] => 9
            [3] => 10
        )

    [amount] => Array
        (
            [0] => 3.00
            [1] => 6.00
            [2] => 9.00
            [3] => 12.00
        )

    [date] => 
    [applicable_states] => 
    [rebate_total_cnt] => 
)

现在我要以表格格式打印上述数据。我该怎么做到这一点。我希望表格格式的数据如下(即表格的标题如下):

pack   quantity    volume  units   amount

数据应显示为来自所有相应数组的数据,相同的索引应显示在一行中。下一行的下一个索引数据,依此类推。那么如何在foreach构建的帮助下实现这一目标呢?提前谢谢。

1 个答案:

答案 0 :(得分:1)

{foreach from=$preview_data.pack key='index' item='value'}
<tr>
   <td>{$value}</td>
   <td>{$preview_data.quantity[$index]}</td>
   <td>{$preview_data.volume[$index]}</td>
   <td>{$preview_data.units[$index]}</td>
   <td>{$preview_data.amount[$index]}</td>
</tr>
{/foreach}