如何在smarty中打印数组?

cod*_*esh 4 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);
Run Code Online (Sandbox Code Playgroud)

在模板文件中:

{foreach from= $team_record key=team item=trecord}
{$trecord[$key].name}
{/foreach}
Run Code Online (Sandbox Code Playgroud)

结果输出必须是“doge_finder”,但我得到了数组中每条记录的第一个初始字符,即。“1 d 5 t 2”

我该如何解决这个问题?

小智 5

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

在这种情况下,您应该使用以下方法打印名称:

{$team_record.name}
Run Code Online (Sandbox Code Playgroud)

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

{foreach from=$team_record key=team item=record}
    {$record.name}
{/foreach}
Run Code Online (Sandbox Code Playgroud)


小智 5

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

输出:

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

我们可以使用下面的代码来迭代Smarty 文件中的数组

{foreach from=$team_record key=arrayIndex item=trecord}
   {$arrayIndex}: {$trecord}
   <br>
{/foreach}
Run Code Online (Sandbox Code Playgroud)

输出 :

       id: 1

       name: doge_finder

       total_rewards: 52.00524500

       desciption: team is only of doge miners

       created_by: 20 
Run Code Online (Sandbox Code Playgroud)