如何从smarty中的关联数组访问和循环内部数组的元素?

时间:2014-05-05 13:31:43

标签: php arrays smarty associative-array

我跟随从PHP分配给智能模板的名为preview_data的关联数组:

Array
(
    [op] => preview
    [id] => 
    [form_submitted] => yes
    [company_id] => 46
    [1] => Array
        (
            [product_id_1] => Array
                (
                    [1] => 8
                    [2] => 11
                )

            [pack] => 10
            [quantity] => 20
            [volume] => 30
            [units] => 7
            [amount] => 40
            [rebate_start_date] => 2014-05-02
            [rebate_expiry_date] => 2014-05-31
            [applicable_states] => Array
                (
                    [0] => 1
                    [1] => 8
                    [2] => 16
                    [3] => 23
                )

            [rebate_total_count] => 5000
        )

    [2] => Array
        (
            [pack] => 100
            [quantity] => 200
            [volume] => 300
            [units] => 9
            [amount] => 80
            [rebate_start_date] => 2014-06-01
            [rebate_expiry_date] => 2014-06-30
            [applicable_states] => Array
                (
                    [0] => 32
                    [1] => 39
                    [2] => 43
                    [3] => 47
                    [4] => 49
                )

            [rebate_total_count] => 10000
            [product_id_2] => Array
                (
                    [1] => 9
                    [2] => 10
                )

        )

    [3] => Array
        (
            [pack] => 500
            [quantity] => 1000
            [volume] => 1500
            [units] => 10
            [amount] => 2000
            [rebate_start_date] => 2014-08-01
            [rebate_expiry_date] => 2014-09-30
            [applicable_states] => Array
                (
                    [0] => 12
                    [1] => 28
                    [2] => 43
                    [3] => 49
                    [4] => 50
                )

            [rebate_total_count] => 9000
            [product_id_3] => Array
                (
                    [1] => 8
                    [2] => 11
                )

        )

    [multiselect] => 50
)

如何在smarty中访问和循环内部数组元素。我的意思是具有索引1,2,3的数组的元素,...还请解释我如何访问内部数组本身的内部元素(即[applicable_states],[product_id_N])。提前致谢。

1 个答案:

答案 0 :(得分:0)

再次检查int和than循环。

PHP

foreach($array as $key => $innerArray) {
    if(is_int($key)) {
       foreach($innerArray as $innerKey => $innerValue) {
           echo $innerValue['applicable_states'];
       }
    }
}

为Smarty试试这个:

{foreach from=$array key=key item=innerArray}
  {if is_int($key)}
     {foreach from=$innerArray key=innerKey item=innerValue}
        {$innerValue.applicable_states}
     {/foreach}
  {/if}
{/foreach}