我想用smarty生成这样的数组结果['element1','element2','lement3']。我正在使用PHP和smarty。数组由PHP生成,然后由smarty以上述格式或样式输出。我的PHP代码是这样的:
$countries = array('America','Germany','Japan');
我想在Smarty中显示相同的数组内容,但这次的结果应该在Smarty 中显示为这样
['America','Germany', 'Japan']
有人能帮我吗?谢谢
在此处检查foreach循环
PHP代码
$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}
]