您的位置:首页 > 其它

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}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: