2

我想用 smarty 产生一个像这样的数组结果 ['element1','element2','element3'] 。我正在使用 PHP 和 smarty。该数组由 PHP 生成,然后由 smarty 以上述格式或样式输出。我的 PHP 代码是这样的:

$countries = array('America','Germany','Japan');

我想在 Smarty 中显示相同的数组内容,但这次结果应该在 smarty 中显示为 this

['America','Germany', 'Japan']

任何人都可以帮助我吗?谢谢!

4

1 回答 1

6

在此处检查foreach 循环
PHP CODE

$countries = array('America','Germany','Japan');
$smarty->assign('countries', $countries);

智能代码

[
{foreach from=$countries item=country}
    {assign var='total' value=$countries|@count}         {* This stores the total no. of elements in COUNTRIES array in TOTAL variable *}
    {counter assign="count"}                             {* This assigns a counter to COUNT variable *}
    '{$country}'
    {if $count lt $total}
    ,                                                    {* This condition adds ',' after each element of the array until and unless the loop reaches the last element. When the last element reached, this condition won't add ',' *}
    {/if}
{/foreach}
]
于 2012-09-07T02:35:38.097 回答