smarty模板--foreach循环二维数组
2015-12-22 14:30
721 查看
<?php
require_once './libs/Smarty.class.php';
// $title="你好";
$arr=array('a'=>'北京','b'=>'上海','c'=>'天津');
$arr1=array(array('哈尔滨','上海'),array('北京','天津'));
$arr2=array(array('a'=>'北京','b'=>'上海','c'=>'天津'),array('c'=>'c北京','d'=>'d上海','c'=>'天津'));
$emplist=array(array('id'=>100,'name'=>"小明","age"=>40),array("id"=>'110',"name"=>"大明","age"=>80),array("id"=>111,"name"=>"中明","age"=>60));
$smarty = new Smarty();
$smarty->assign("arr",$arr);
$smarty->assign("arr1",$arr1);
$smarty->assign("arr2",$arr2);
$smarty->assign("emplist",$emplist);
$smarty->assign("if",5);
$smarty->display("text.tpl");
?>
.tpl 页面
<br/><h1>*从from中取出数组指向temp*</h1><br/>
{foreach from=$arr item=temp key=lx}
{$lx}={$temp}
{/foreach}
<br/><h1>***取出二维数组***</h1><br/>
{foreach from=$arr1 item=temp}
{foreach from=$temp item=val}
{$val}
{/foreach}<br/>
{/foreach}
<br/><h1>***取出二维关联数组***</h1><br/>
{foreach from=$arr2 item=temp}
{foreach from=$temp item=val key=lx}
{$lx}={$val}
{/foreach}<br/>
{/foreach}
{foreach from=$emplist item=temp}
{foreach from=$temp item=val key=lx}
{if $lx!=age}
{$lx}={$val}
{/if}
{/foreach}<br/>
{/foreach}
<br/><h1>***IFELSE***</h1><br/>
{if $if>10}
a>10
{else}
a<10
{/if}
{foreach from=$emplist item=temp}
{if $temp.age<50}
小孩{$temp.name}年龄为{$temp.age}<br/>
{elseif50<$temp.age&&$temp.age<70}
青年朋友{$temp.name}年龄为{$temp.age}
{else}
老年朋友{$temp.name}年龄为{$temp.age}<br/>
{/if}
{/foreach}
require_once './libs/Smarty.class.php';
// $title="你好";
$arr=array('a'=>'北京','b'=>'上海','c'=>'天津');
$arr1=array(array('哈尔滨','上海'),array('北京','天津'));
$arr2=array(array('a'=>'北京','b'=>'上海','c'=>'天津'),array('c'=>'c北京','d'=>'d上海','c'=>'天津'));
$emplist=array(array('id'=>100,'name'=>"小明","age"=>40),array("id"=>'110',"name"=>"大明","age"=>80),array("id"=>111,"name"=>"中明","age"=>60));
$smarty = new Smarty();
$smarty->assign("arr",$arr);
$smarty->assign("arr1",$arr1);
$smarty->assign("arr2",$arr2);
$smarty->assign("emplist",$emplist);
$smarty->assign("if",5);
$smarty->display("text.tpl");
?>
.tpl 页面
<br/><h1>*从from中取出数组指向temp*</h1><br/>
{foreach from=$arr item=temp key=lx}
{$lx}={$temp}
{/foreach}
<br/><h1>***取出二维数组***</h1><br/>
{foreach from=$arr1 item=temp}
{foreach from=$temp item=val}
{$val}
{/foreach}<br/>
{/foreach}
<br/><h1>***取出二维关联数组***</h1><br/>
{foreach from=$arr2 item=temp}
{foreach from=$temp item=val key=lx}
{$lx}={$val}
{/foreach}<br/>
{/foreach}
{foreach from=$emplist item=temp}
{foreach from=$temp item=val key=lx}
{if $lx!=age}
{$lx}={$val}
{/if}
{/foreach}<br/>
{/foreach}
<br/><h1>***IFELSE***</h1><br/>
{if $if>10}
a>10
{else}
a<10
{/if}
{foreach from=$emplist item=temp}
{if $temp.age<50}
小孩{$temp.name}年龄为{$temp.age}<br/>
{elseif50<$temp.age&&$temp.age<70}
青年朋友{$temp.name}年龄为{$temp.age}
{else}
老年朋友{$temp.name}年龄为{$temp.age}<br/>
{/if}
{/foreach}
相关文章推荐
- 统一设置导航栏样式
- Linq 中 Distinct 方法扩展
- 线程3-线程死锁
- php获取字符串拼音和拼音首字母
- 最新版 使用Xcode6.4 和Instruments、Leaks调试解决iOS内存泄露
- Xshell & Xftp & XManager & MobaXterm
- 正则表达式
- iOS 中LaunchImage 载入界面大小设置
- usbnet驱动深入分析-usb虚拟网卡host端【转】
- 线程2-线程同步
- selenium webdriver 杂记-生成随机数字字母组合参数
- 自定义view模板,实现点击事件的回调
- mybatis自己学习的一些总结
- 博客搬家啦
- PostgreSQL-数据目录与pg_ctl
- Hibernate session FlushMode的五种设置
- Android ListView使用convertView解决内存溢出问题(推介使用recyclerview,listview已过时)
- 学PHP避免即将到来的就业危机
- CSS布局奇淫技巧之-宽度自适应
- NSFileManager—枚举目录