2012-09-07 78 views
2

我想用smarty產生像這樣['element1','element2','element3']的數組結果。我正在使用PHP和smarty。該數組是從PHP生成的,然後由smarty以上述格式或樣式輸出。 我的PHP代碼是這樣的:如何在smarty中顯示數組?

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

我想顯示在Smarty的同一陣列的內容,但這次的結果應該在智者顯示爲這個

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

任何人都可以成爲幫助我?謝謝!

回答

6

檢查foreach loop這裏
PHP代碼

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

SMARTY CODE

[ 
{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} 
]