smarty array index
I have an array
array('foo_1' => 1, 'bar_1' =>1, 'foo_2' => 2, 'bar_2' => 2, ... )
for($i = 1; $i<=12; $i++){
echo $month['foo_'.$i];
}
And I don't understand how can I access the values from it using Smarty.
{counter start=0 skip=1 assign="i"}
{section name = month start = 0 loop = 12 step = 1}
{if isset($arr.开发者_如何学运维foo_.$i)}
{$arr.bar_$i}
{/if}
{counter}
{/section}
But it doesn't work. The main issue is - How I can access array['foo_1'] in smarty ? Can you please help?
You must build the key before accessing it in the array. Use assign to build the complete name.
{assign var=fooKey value="foo_"|cat:$i}
{assign var=barKey value="bar_"|cat:$i}
{if isset($arr.{$fooKey})}
{$arr.{$barKey}}
{/if}
Your attempt {$arr.foo_.$i}
means accessing the subkey $i
in the array under $arr.foo_
.
精彩评论