这是随便写的一个三层循环的代码,其实你可以按照这样来做,应该就没什么问题..
test.php
<?php
require('smarty.php');
/*
$c = array (
array("phone" => "1111", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333", "cell" =>"760-1234")
);
$smarty->assign("contacts", $c);
*/
//$smarty->append('myarray1', $myarray);
/*
$a=array( saddsa=>"gougou", haha=>'sansann', keskjasdsa33kjdawa=>'fafa' );
$smarty->assign("haha",$a);
*/
/* 三层循环代码
$a=array(
array( 'phone' => '1','fax' => '2','cell' => '3'),
array('phone' => '555-4444','fax' => '555-3333','cell' => '760-1234')
);
$smarty->assign('contacts',$a);
*/
$b1c1=array('b1c1 one','b1c1 two','b1c1 tree');
$b1c2=array('b1c2 one','b1c2 two','b1c2 tree');
$b2c1=array('b2c1 one','b2c1 two','b2c1 tree');
$b2c2=array('b2c2 one','b2c2 two','b2c2 tree');
$b1=array( '第二层的一1'=>$b1c1,'第二层的一2'=>$b1c2 );
$b2=array( '第二层的二1'=>$b2c1,'第二层的二2'=>$b2c2 );
$a=array( '第一层的一'=>$b1,"第一层的二"=>$b2 );
$smarty->assign("xun",$a);
$ccc=array('fsefe','sefsesefes');
$smarty->assign('aaa',$ccc);
//$smarty->assign("xun2",$a[0]);
//$smarty->assign("",$a[0]);
$smarty->display('test.tpl');
?>
smarty.php
<?php
require('./smarty/libs/smarty.class.php'); //包含smarty类文件
$smarty=new Smarty; //建立smarty实例对象$smarty
$smarty->template_dir="./smarty/templates"; //设置模板目录
$smarty->compile_dir="./smarty/templates_c"; //设置编译目录
$smarty->config_dir="./smarty/configs/"; //设置配置目录
$smarty->cache_dir="./smarty/cache/"; //设置缓存目录
//----------------------------------------------------
//左右边界符,默认为{},但实际应用当中容易与JavaScript
//相冲突,所以建议设成<{}>或其它。
//----------------------------------------------------
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
?>
test.tpl
<?php
这只是一个测试
<br />
<!--
{foreach name=outer item=contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}<br>
{/foreach}
{/foreach}
-->
<!--
{foreach from=$haha item=heihei key=k}
{$k}:{$heihei}<br />
{/foreach}
-->
<!-- 三层循环代码
{foreach name=outer item=contact from=$contacts}
<hr />
{foreach key=key item=item from=$contact}
{$key}: {$item}<br />
{/foreach}
{/foreach}
-->
{foreach from=$xun item=ha1 key=k1}
<p>{$k1}<br />
{foreach from=$ha1 item=ha2 key=k2}
{$k2}<br />
{foreach from=$ha2 item=ha3}
这是第三层{$ha3}<br />
{/foreach}
{/foreach}
<p>
{/foreach}
<p><p><p>
显示一下下<br />
{foreach from=$aaa item=hei}
{$hei}<br />
{/foreach}
<br />
<br />
循环完毕,打完收工
?>