{foreachelse} nothing {/foreach} 显示的结果是 一维key=0 一维value=home 一维key=1 一维value=who 一维key=2 一维value=tank 一维key=3 一维value=what b),section来读取 查看复制打印? {section name=one loop=$onearray start=0 step=1} index={$smarty.section.one.index}, index_prev={$smarty.section.one.index_prev}, index_next={$smarty.section.one.index_next}, first={$smarty.section.one.first}, last={$smarty.section.one.last}, iteration ={$smarty.section.one.iteration}, total={$smarty.section.one.total}, value={$onearray[one]}
{sectionelse} nothing {/section} 显示的结果是 index=0, index_prev=-1, index_next=1, first=1, last=, iteration =1, total=4, value=home index=1, index_prev=0, index_next=2, first=, last=, iteration =2, total=4, value=who index=2, index_prev=1, index_next=3, first=, last=, iteration =3, total=4, value=tank index=3, index_prev=2, index_next=4, first=, last=1, iteration =4, total=4, value=what 2,二维数组 查看复制打印? array ( "test" => 'home', '2' => 'who', array ( "上海", "born" => "安徽", "name" => "海底苍鹰" ), array ( "1583456", "fax" => "12345678", "cell" => "13256478414" ) ); $this->tpl->assign("twoarray", $this->twoarray); a),foreach 查看复制打印? {foreach from=$twoarray kkey=k item=value } {if is_array($value)} {foreach from=$value key=tk item=tv } 二维tkey={$tk} 二维value={$tv}
{foreachelse} 二维数组为空 {/foreach} {else} 一维key={$k} 一维value={$value}
{/if} {foreachelse} nothing {/foreach} 显示结果如下: 一维key=test 一维value=home 一维key=2 一维value=who 二维tkey=0 二维value=上海 二维tkey=born 二维value=安徽 二维tkey=name 二维value=海底苍鹰 二维tkey=0 二维value=1583456 二维tkey=fax 二维value=12345678 二维tkey=cell 二维value=13256478414 b),section 查看复制打印? {section loop=$twoarray name=two } {if is_array($twoarray[two])} {section loop=$twoarray[two] name=aaa } 二维tkey={$smarty.section.aaa.index} 二维value={$twoarray[two][aaa]}
{sectionelse} 二维数组为空 {/section} {else} 一维key={$smarty.section.two.index} 一维value={$twoarray[two]}
{/if} {sectionelse} nothing {/section} 显示结果如下: 一维key=0 一维value= 一维key=1 一维value= 一维key=2 一维value=who 二维tkey=0 二维value=上海 二维tkey=1 二维value= 二维tkey=2 二维value= 如果是一维数组,并且带有下标的话,并且不是按0,1,2这样的顺序的话就取不到东西。这个能不能理解成foreach存在的理由。不过一般从数据库取数据都是二维数组,并且外层都是以0开始的。看下面的这个例子 查看复制打印? array ( array ( 'name' => 'tank', 'sex' => '男', 'old' => '28' ), array ( 'name' => 'joyce', 'sex' => '女', 'old' => '111' ) ); section循环 查看复制打印? {section loop=$twoarray name=two} name={$twoarray[two].name},sex={$twoarray[two].sex},old={$twoarray[two].old}
{sectionelse} nothing {/section} 显示结果 name=tank,sex=男,old=28 name=joyce,sex=女,old=111