Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4175967
  • 博文数量: 601
  • 博客积分: 15410
  • 博客等级: 上将
  • 技术积分: 6884
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-16 08:11
个人简介

独学而无友,则孤陋而寡闻!

文章分类

全部博文(601)

文章存档

2020年(1)

2018年(4)

2017年(7)

2016年(42)

2015年(25)

2014年(15)

2013年(36)

2012年(46)

2011年(117)

2010年(148)

2009年(82)

2008年(37)

2007年(41)

分类: Python/Ruby

2011-07-22 11:00:35

网上的资料大多数都是用foreach,而smarty手册中,自带了一个section的例子:

  1. Example 7-19. sections and associative arrays
  2. 例 7-19. section 遍历多维数组演示

  3. {* This is an example of printing an associative array
  4.  of data within a section *}
  5. {section name=customer loop=$contacts}
  6.     name: {$contacts[customer].name}<br>
  7.     home: {$contacts[customer].home}<br>
  8.     cell: {$contacts[customer].cell}<br>
  9.     e-mail: {$contacts[customer].email}<p>
  10. {/section}


  11. OUTPUT:

  12. name: John Smith<br>
  13. home: 555-555-5555<br>
  14. cell: 555-555-5555<br>
  15. e-mail: john@mydomain.com<p>
  16. name: Jack Jones<br>
  17. home phone: 555-555-5555<br>
  18. cell phone: 555-555-5555<br>
  19. e-mail: jack@mydomain.com<p>
  20. name: Jane Munson<br>
  21. home phone: 555-555-5555<br>
  22. cell phone: 555-555-5555<br>
  23. e-mail: jane@mydomain.com<p>
解释:
  $contacts每次循环,都用customer来替换,而customer就是name中定义的名称,name则是customer的一个属性。

再看foreache的方案:

  1. <{foreach from=$myarr item=arr1 name=thearr1}>
  2. 2    <{foreach from=$arr1 item=arr2}>
  3. 4     <{foreach from=$arr2 item=single key=key}>
  4. 5     &nbsp;<{$key}><{$single}><br />
  5. 6     <{/foreach}>
  6. 7     <{/foreach}>
  7. 8    <{/foreach}>






阅读(1578) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~