2,foreach 必须和 /foreach 成对使用,且必须指定 from 和 item 属性.
3,foreach可以圈套不过name必须为一
4,from 待循环数组的名称,key数组下标,item当前处理元素的变量名称,name该循环的名称,用于访问该循环
5,foreachelse 语句在 from 后面的数组对像没有值的时候被执行.
2,loop循环的变量名称,必须有的选项
3,start 循环开始的KEY值 ,默认是从第一个开始,
4,step设置循环的跳跃数,step=2将只遍历下标为0、2、4等的元素.
5,max循环的次数,默认为1,
6,show是不是显示该循环
{$array[arrayname.index]} ,{$array[arrayname]}二者是等价的,由起可见,有了上面的a-h是多么的重要
- array (
- "0" => 'home',
- '1' => 'who',
- '2'=> 'tank',
- '3'=> 'what'
- );
- $this->tpl->assign("onearray", $this->onearray);
array ( "0" => 'home', '1' => 'who', '2'=> 'tank', '3'=> 'what' );$this->tpl->assign("onearray", $this->onearray);
- {foreach from=$onearray kkey=k item=value }
- 一维key={$k} 一维value={$value}
- {foreachelse}
- nothing
- {/foreach}
{foreach from=$onearray key=k item=value } 一维key={$k} 一维value={$value}
{foreachelse} nothing {/foreach}
一维key=1 一维value=who
一维key=2 一维value=tank
一维key=3 一维value=what
- {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}
{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=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
- array (
- "test" => 'home',
- '2' => 'who',
- array (
- "上海",
- "born" => "安徽",
- "name" => "海底苍鹰"
- ),
- array (
- "1583456",
- "fax" => "12345678",
- "cell" => "13256478414"
- )
- );
- $this->tpl->assign("twoarray", $this->twoarray);
array ( "test" => 'home', '2' => 'who', array ( "上海", "born" => "安徽", "name" => "海底苍鹰" ), array ( "1583456", "fax" => "12345678", "cell" => "13256478414" ) );$this->tpl->assign("twoarray", $this->twoarray);
- {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}
{foreach from=$twoarray key=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=2 一维value=who
二维tkey=0 二维value=上海
二维tkey=born 二维value=安徽
二维tkey=name 二维value=海底苍鹰
二维tkey=0 二维value=1583456
二维tkey=fax 二维value=12345678
二维tkey=cell 二维value=13256478414
- {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}
{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=1 一维value=
一维key=2 一维value=who
二维tkey=0 二维value=上海
二维tkey=1 二维value=
二维tkey=2 二维value=
- array (
- array (
- 'name' => 'tank',
- 'sex' => '男',
- 'old' => '28'
- ),
- array (
- 'name' => 'joyce',
- 'sex' => '女',
- 'old' => '111'
- )
- );
array ( array ( 'name' => 'tank', 'sex' => '男', 'old' => '28' ), array ( 'name' => 'joyce', 'sex' => '女', 'old' => '111' ) );
- {section loop=$twoarray name=two}
- name={$twoarray[two].name},sex={$twoarray[two].sex},old={$twoarray[two].old}
- {sectionelse}
- nothing
- {/section}
{section loop=$twoarray name=two} name={$twoarray[two].name},sex={$twoarray[two].sex},old={$twoarray[two].old}
{sectionelse} nothing {/section}
name=joyce,sex=女,old=111