3

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.

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
pawel-kuznik
  • 437
  • 4
  • 11

3 Answers3

4

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.

lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
3

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"]}
Meloman
  • 3,558
  • 3
  • 41
  • 51
0

This will work also

{if $smarty.get["param_{$k.id}_{$p.num}"] eq $key}
  ...
unloco
  • 6,928
  • 2
  • 47
  • 58