代码说

code is poetry

代码说    
碎碎念:和我一起翱翔在那片无边的天际,一起看见那许多命运惊奇和神秘般的人生绮丽 --- 街角的蔷薇。  换一换

smarty遍历数组的两种方式

作者:coderzheng 发布于:2015-5-6 1:41 Wednesday 分类:other  阅读模式

<?php
// PHP端
// smarty主文件引入(省略)

$atcs = array(
     "0" => array(
        "tid" => 21,
        "updated" => "2007-12-31 10:21:18",
        "created" => "2007-12-31 10:21:18",
        "typename" => "体系相当",
        "fid" => 0,
    ),
    "1" => array(
        "tid" => 20,
        "updated" => "2007-12-31 10:21:07",
        "created" => "2007-12-31 10:21:07",
        "typename" => "html/css/ajax",
        "fid" => 0,
        "smalltype" => array(
            "0" => array(
                "tid" => 25,
                "updated" => "2007-12-31 10:25:42",
                "created" => "2007-12-31 10:25:42",
                "typename" => "htmlbook",
                "fid" => 20,
                "topArticle" => array(
                    "0" => array(
                        "atcid" => "9",
                        "title" => "文章测试",
                        "tid" => 25,
                        "created" => "2007-12-31 10:27:13" ,
                        "hits" => 1,
                        "atc_guest" => '',
                    ),
                ),
            ),
        ),
    ),
    "2" => array(
        "tid" => 17,
        "updated" => "2007-12-31 10:20:34",
        "created" => "2007-12-31 07:55:34",
        "typename" => "PHP编程",
        "fid" => 0,
        "smalltype" => array(
            "0" => array(
                "tid" => 24,
                "updated" => "2007-12-31 10:22:16",
                "created" => "2007-12-31 10:22:16",
                "typename" => "fleaphp框架",
                "fid" => 17,
                "topArticle" => '',
            ),
            "1" => array(
                "tid" => 23,
                "updated" => "2007-12-31 10:21:50",
                "created" => "2007-12-31 10:21:50",
                "typename" => "PHP代码库",
                "fid" => 17,
                "topArticle" => array(
                    "0" => array(
                        "atcid" => 13,
                        "title" => "特别帖",
                        "tid" => 23,
                        "created" => "2008-02-26 09:59:25",
                        "hits" => 1,
                        "act_guest" => '',
                    ),
                ),
            ),
            "2" => array(
                "tid" => 22,
                "updated" => "2007-12-31 10:21:33",
                "created" => "2007-12-31 10:21:33",
                "typename" => "PHP根蒂根基",
                "fid" => 17,
                "topArticle" => array(
                    "0" => array(
                        "atcid" => 12,
                        "title" => "smarty生成分页静态页面",
                        "tid" => 22,
                        "created" => "2007-12-31 10:40:08",
                        "hits" => 1,
                        "atc_guest" => '',
                    ),
                    "1" => array(
                        "atcid" => l1,
                        "title" => "smarty生成静态页面",
                        "tid" => 22,
                        "created" => "2007-12-31 10:36:28",
                        "hits" => 1,
                        "atc_guest" => '',
                    ),
                ),
            ),
        ),
    ),
);

$lists = array(
    "0" => array(
        "agent" => "111",
        "info" => array(
            "name" => "name1",
            "title" => "title1",
            "link" => "link1",
        ),
    ),
    "1" => array(
        "agent" => "222",
        "info" => array(
            "name" => "name2",
            "title" => "title2",
            "link" => "link2",
        ),
    ),
);

$smarty->assign("atcs", $arr);
$smarty->assign("lists", $lists);
$smarty->display("admin/site/test.tpl");
HTML端

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<!-- tpl 端 -->
<{section name=k loop=$atcs}>

<div align="left"><{$atcs[k].typename}></div> <!-- 一级类别 -->

<{assign var=smalltype value=$atcs[k].smalltype}>
<{section name=kk loop=$smalltype}> <!-- 二级类别 -->
<div align="left">&nbsp;--> <{$smalltype[kk].typename}> </div>

<{assign var=topAtc value=$smalltype[kk].topArticle}> <!-- 二级分类中的几篇文章 -->
<{section name=kkk loop=$topAtc}>
<div align="left">&nbsp;&nbsp;&nbsp;------<{$topAtc[kkk].title}>------<{$topAtc[kkk].created}></div>
<{/section}> <!-- 二级分类文章结束 -->

<{/section}> <!-- 二级类别遍历结束 -->
<hr />
<{/section}> <!-- 一级类别遍历结束 -->

<br /><br />

<!-- 另一种遍历方式 -->
<{foreach key=k item=i from=$atcs}>

<div align="left"><{$i.typename}></div><!-- 一级类别 -->

    <{foreach key=kk item=ii from=$i.smalltype}> <!-- 二级类别 -->
        <div align="left">&nbsp;--> <{$ii.typename}> </div>
       
        <{foreach key=kkk item=iii from=$ii.topArticle}> <!-- 二级分类中的几篇文章 -->
            <div align="left">&nbsp;&nbsp;&nbsp;------<{$iii.title}>------<{$iii.created}></div>
        <{/foreach}>
    <{/foreach}>
    <hr />
<{/foreach}>

<br /><br />

<{foreach key=k item=i from=$lists}>
    <div><{$i.agent}></div>
    <{foreach key=kk item=ii from=$i.info}>
        <div>&nbsp;&nbsp;--> <{if $kk == "name"}><{$ii}><{/if}></div>
    <{/foreach}>
<{/foreach}>

<!-- 当键名不是0,1,2,3,...这种有序自然数时, 如果在遍历的过程中要用到键名, 这种情况只能使用foreach而不能用section, -->
</body>
</html>

标签: smarty遍历数组

你可以发表评论、引用到你的网站或博客,或通过RSS 2.0订阅这个博客的所有文章。
上一篇: 成为更专注的开发者  |  下一篇:记录一下最近碰到的烦心事