2014-03-27 71 views
1

我有一個數組:如何在smarty中打印數組?

$team_details = Array ([id] => 1 [name] => doge_finder [total_rewards] => 52.00524500 [desciption] => team is only of doge miners [created_by] => 20); 

/* assigning to a smarty template */ 
$smarty->assign("team_record", $team_details);   
$smarty->display($tpl); 

在模板文件:

{foreach from= $team_record key=team item=trecord} 
{$trecord[$key].name} 
{/foreach} 

在結果輸出必須是「doge_finder」,但我得到的陣列,即每個記錄的首字符。 「1 d 5 t 2」

我該如何解決這個問題?

+0

心不是牛逼他的{$ team_record.name}'夠了嗎? –

回答

2

如果你只是想打印一個關聯數組,你不需要用戶{foreach}。只需使用$array.key格式。

在這種情況下,你應該打印使用

{$team_record.name} 

如果您有多個關聯數組。您可以使用:

{foreach from=$team_record key=team item=record} 
    {$record.name} 
{/foreach} 
+0

謝謝,它工作正常。因爲我有一個關聯數組,所以我寫了{foreach}來打印該數組。 – codeBYmahesh

+0

如果存在多維數組,例如:Array([0] => Array([id] => 1 [name] => doge_finder [total_rewards] => 52.00524500 [desciption] =>團隊僅限於doge礦工[created_by] => 20 [created_date] =>)[1] =>陣列([id] => 2 [name] =>延遲礦工[total_rewards] => 45.25462000 [desciption] =>團隊成員是晚期投幣礦工[created_by] => 20 [created_date] =>)) 那麼我可能知道怎麼才能在這個數組中打印名稱字段? – codeBYmahesh

+1

請參閱我編輯的答案。 – fian

0

我們可以使用{$team_record|print_r}到dsiplay 整個數組中的智者文件。

輸出:

Array 
(
    [id] => 1     
    [name] => doge_finder 
    [total_rewards] => 52.00524500 
    [desciption] => team is only of doge miners 
    [created_by] => 20 
) 

我們可以使用下面的代碼來迭代在Smarty的數組文件

{foreach from=$team_record key=arrayIndex item=trecord} 
    {$arrayIndex}: {$trecord} 
    <br> 
{/foreach} 

輸出:

 id: 1 

     name: doge_finder 

     total_rewards: 52.00524500 

     desciption: team is only of doge miners 

     created_by: 20