您有 2 种可能的解决方案。
第一个解决方案
在 PHP 中你使用:
$data = '[
{
"first_name": "jinu",
"last_name": "mk",
"loginid": "[email protected] /cdn-cgi/l/email-protection",
"timezone": "5.5",
"team_id": "c964ef415f157ddd99173f5b481ee1e3",
"user_type": 1,
"last_login_date": null
},
{
"first_name": "avatar second",
"last_name": "test",
"loginid": "[email protected] /cdn-cgi/l/email-protection",
"timezone": "5.5",
"team_id": "ec40f5feda8643135bc20be44f897b03",
"user_type": "3",
"last_login_date": null
},
{
"first_name": "avatar testing admin",
"last_name": "amt 1",
"loginid": "[email protected] /cdn-cgi/l/email-protection",
"timezone": "5.5",
"team_id": "ec40f5feda8643135bc20be44f897b03",
"user_type": 1,
"last_login_date": null
}
]';
$smarty->assign('games',$data);
在 Smarty 中,您使用:
{foreach from=$games|json_decode item=foo}
<li>{$foo->first_name}</li>
{/foreach}
但是我不确定在这种情况下是否json_decode
运行于$games
仅一次或每次调用时。
第二种解决方案
在 PHP 中你使用:
$data = '[
{
"first_name": "jinu",
"last_name": "mk",
"loginid": "[email protected] /cdn-cgi/l/email-protection",
"timezone": "5.5",
"team_id": "c964ef415f157ddd99173f5b481ee1e3",
"user_type": 1,
"last_login_date": null
},
{
"first_name": "avatar second",
"last_name": "test",
"loginid": "[email protected] /cdn-cgi/l/email-protection",
"timezone": "5.5",
"team_id": "ec40f5feda8643135bc20be44f897b03",
"user_type": "3",
"last_login_date": null
},
{
"first_name": "avatar testing admin",
"last_name": "amt 1",
"loginid": "[email protected] /cdn-cgi/l/email-protection",
"timezone": "5.5",
"team_id": "ec40f5feda8643135bc20be44f897b03",
"user_type": 1,
"last_login_date": null
}
]';
$smarty->assign('games',json_decode($data));
在 Smarty 文件中:
{foreach from=$games item=foo}
<li>{$foo->first_name}</li>
{/foreach}
我总是建议使用第二种方法,因为如果可能的话,在 Smarty 中您应该避免使用任何计算而只显示数据。