smarty模板引擎使用内建函数foreach循环取出所有数组值的方法 |
本文实例讲述了smarty内建函数foreach的使用方法,分享给大家供大家参考 。具体如下: 显示文件:index.php: 复制代码 代码如下: <?php
//创建smarty对象 require_once("./libs/Smarty.class.php"); $smarty = new Smarty(); $arr1 = array("北京","上海","广州");//索引数组 $smarty->display("temp.tpl"); 模板文件:temp.tpl 复制代码 代码如下: <html>
<h2>smarty内建函数foreach,循环取出数组值</h2> <p style="color:green">实例1:一维索引数组</p> {foreach from=$arr1 item=temp} {$temp} {/foreach} <p style="color:orange">实例2:一维关联数组——>item为键值,key为键名 。如果不取key,取出方法与一维索引数组相同,当然索引数组也是有key的0,1,2...</p> <p style="color:red">实例3:二维索引数组——>两次循环即可</p> <p style="color:red">实例4:二维关联数组——>同样两次循环即可</p> </html> 希望本文所述对大家的php程序设计有所帮助 。 |