smarty模板引擎使用内建函数foreach循环取出所有数组值的方法
本文实例讲述了smarty内建函数foreach的使用方法,分享给大家供大家参考。具体如下:
显示文件:index.php:
<?php //创建smarty对象 require_once("./libs/Smarty.class.php"); $smarty=newSmarty();
$arr1=array("北京","上海","广州");//索引数组 $smarty->assign("arr1",$arr1);//分配索引数组 $arr2=array("city1"=>"北京","city2"=>"上海","city3"=>"广州");//关联数组 $smarty->assign("arr2",$arr2);//分配关联数组 $arr3=array(array("北京","上海","广州"),array("关羽","张飞","美女"));//二维索引数组 $smarty->assign("arr3",$arr3); $arr4=array(array("c1"=>"北京","c2"=>"上海","c3"=>"广州"),array("n1"=>"关羽","n2"=>"张飞","n3"=>"美女"));//二维关联数组 $smarty->assign("arr4",$arr4);
$smarty->display("temp.tpl"); ?>