如何在smarty中打印数组?

时间:2014-03-27 05:56:37

标签: php arrays foreach 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"

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

如果您只想打印一个关联数组,则无需用户{foreach}。只需使用$array.key格式。

在这种情况下,您应该使用

进行打印
{$team_record.name}

如果你有多个关联数组。您可以使用:

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

答案 1 :(得分:3)

我们可以使用{$team_record|print_r}在smarty文件中隐藏整个数组。

<强>输出:

 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