smarty模板--foreach循环二维数组

<?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}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值