1

I have a smarty variable which stores an associative array. Is there a way to combine a string and another smarty variable to create the index?

Example: The array element I want to print is $foo['index1'] and the variable $count = 1. I tried to print the value using $foo.index$count but that results in a smarty syntax error (unrecognized tag).

2 Answers 2

1

If you have something like this in PHP:

$foo['index1'] = 'aaa';
$smarty->assign('foo', $foo);
$smarty->assign('count', 1);

Then you should do in smarty:

{assign var = "idx" value = "index"|cat:$count}
{$foo[$idx]}
Sign up to request clarification or add additional context in comments.

Comments

0

Like this you can get the index count in foreach:

{foreach from=$value item=foo key=k name=myloop}                                
{assign var=op value=$smarty.foreach.myloop.index}
index count:{$op}
{/foreach}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.