dynamic array key in smarty

2019-02-19 12:19发布

问题:

I just want to know how to access an array in SMARTY that have generated index via smarty. I want to access it later.

Got something like this:

{if $smarty.get.{param_{$k.id}_{$p.num}} eq $key}

and I like to access $smarty.get.(dynamic generated index)

Tried a couple variations, but didn't have any luck.

回答1:

You can use variable for indexes, but you cannot build string the way you tried. Try to build a custom variable before:

{$myIndex = "param_{$k.id}_{$p.num}"}
{if $smarty.get.$myIndex eq $key}
    ...

This should work.



回答2:

In my case I used UnLoCo solution and tried to directly access array content (without if statement) with dynamic key and it works perfectly :

{$arr1["column_{$arr2.month}_price"]}


回答3:

This will work also

{if $smarty.get["param_{$k.id}_{$p.num}"] eq $key}
  ...