Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic array key in smarty

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.

like image 689
pawel-kuznik Avatar asked Oct 31 '25 15:10

pawel-kuznik


2 Answers

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.

like image 53
lorenzo-s Avatar answered Nov 02 '25 05:11

lorenzo-s


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"]}
like image 36
Meloman Avatar answered Nov 02 '25 07:11

Meloman