Search code examples
arrayssmartyassociative-arraysmarty2

What's the issue in accessing associative array element in smarty in following scenario?


I've following array title $value which I've assigned to smarty template.

 Array
        (
            [id] => 175
            [start_date] => 2014-07-01
            [end_date] => 2014-07-31
            [count] => 3
            [rebate_type_id] => 2
            [created_at] => 1406031299
            [updated_at] => 1406031299
            [applicable_states] => Array
                (
                    [0] => Array
                        (
                            [state_id] => 0
                            [rebate_id] => 175
                            [state_name] => All States
                        )

                )
            [manufacturer_id] => 49
            [company_name] => Blue Nun
            [manufacturer_image_path] => 
        )

For accessing the inner array element I wrote following code but it's not working. Can you please correct the mistake I made in below code?

{if $value.appplicable_states.0.state_name == 'All States'}
                  All States
                {/if}

Solution

  • When in PHP you made such assignment (I've simplified array):

    $data= array('applicable_states' => array(0 => array('state_name' => 'All States')));
    $smarty->assign('value',$data);
    

    in Smarty you can use:

    {if $value.applicable_states.0.state_name =='All States'}
    All States
    {else}
    Not All States
    {/if}
    

    Output for this is:

    All States