Question

I've following associative array titled preview_data which is assigned from PHP to smarty template:

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
)

How to access and loop over inner array elements in smarty. I mean the elements of arrays having index 1,2,3,... Also please explain me how to access the inner elements of inner arrays itself(i.e.[applicable_states], [product_id_N]). Thanks in advance.

Was it helpful?

Solution

Check for an int and than loop again.

PHP

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

Try this for 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}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top