Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smarty assign value to an array variable index

Tags:

php

smarty

I am using SMARTY and I need to create an array and assign value to a particular index of it.

Something like:

{foreach from=$a key='i' item='b'}
//Some calculations here giving me a VALUE
ARRAY[$i] = $VALUE;
{/foreach}

Now the problem is when I am using standard Smarty Assign syntax

{assign var='array.$i' value=$VALUE}
{assign var='array[$i]' value=$VALUE}
{assign var=$array.$i value=$VALUE}
{assign var='$array[$i]' value=$VALUE}

its not working. I need to use this array later in the code and hence need it in an array format only

like image 559
Sparsh Gupta Avatar asked May 14 '11 14:05

Sparsh Gupta


People also ask

How do you assign a value to an index array?

Assigning values to an element in an array is similar to assigning values to scalar variables. Simply reference an individual element of an array using the array name and the index inside parentheses, then use the assignment operator (=) followed by a value.

What is Smarty variable?

Variables in Smarty can be either displayed directly or used as arguments for functions, attributes and modifiers, inside conditional expressions, etc. To print a variable, simply enclose it in the delimiters so that it is the only thing contained between them. Example 4.1. Example variables.


1 Answers

Have you tried the assign shorthand instead?

{$array.$i = $VALUE}
like image 90
onteria_ Avatar answered Oct 28 '22 18:10

onteria_