2016-07-31 86 views
0

我的数组:Smarty的:使用一个变量数组以得到下一个索引

$array[0].item = '1' 
$array[1].item = '2' 
$array[2].item = '3' 
... 

现在我想要得到下一个索引的价值:(计数器梅索德)

{counter assign=i start=1 print=false} 
{foreach $array as $myArray} 
    var currentValue = {$myArray.item}; // currentValue = 1 
    var nextValue = {$array[$i].item}; // doesn't work 
    {counter} 
{/foreach} 

变量$我是在第一个循环完全'1',但我不能用它作为索引数组,为什么?

回答

0

尝试使用{section}http://www.smarty.net/docsv2/en/language.function.section.tpl

{counter start=1 print=false} 
{section name=i loop=$myArray} 
    var currentValue = {$myArray[i].item}; 
    var nextValue = {$myArray[i.index_next].item|default:''}; 
    {counter} 
{/section} 

确保在你的PHP代码,你正在做正确的分配:

$smarty->assign('myArray', $array);