Smarty section foreach traverses multi-dimensional arrays

Source: Internet
Author: User
Tags php basics

Note:

Section is generally used to indicate that the key number is a continuous number.
Array (
0 => 1,
1 => 2
)

If your key number is a string, you must use foreach to implement it.
Array (
'Xxx' => 1,
'Yyy' => 2
)

  • Multi-dimensional array section instance

For example, the dataset is:

Array ([0] => array ([TID] => 21 [updated] => 10:21:18 [created] => 10:21:18 [typename] => system related [FID] => 0) [1] => array ([TID] => 20 [updated] => 10:21:07 [created] => 10:21:07 [typename] => html/CSS/ajax [FID] => 0 [smalltype] => array ([0] => array ([TID] => 25 [updated] => 10:25:42 [created] => 10:25:42 [typename] => htmlboo K [FID] => 20 [toparticle] => array ([0] => array ([atcid] => 9 [title] => Article test [TID] => 25 [created] => 10:27:13 [Hits] => 1 [atc_guest] => ))))) [2] => array ([TID] => 17 [updated] => 10:20:34 [created] => 07:55:34 [typename] => PHP programming [FID] => 0 [smalltype] => array ([0] => array ([TID] => 24 [updated] => 10:22:16 [created] =>: 16 [typename] => fleaphp framework [FID] => 17 [toparticle] => array ()) [1] => array ([TID] => 23 [updated] => 10:21:50 [created] => 10:21:50 [typename] => PHP code library [FID] => 17 [toparticle] => array ([0] => array ([atcid] => 13 [title] => special post [TID] => 23 [created] => 09:59:25 [Hits] => 1 [atc_guest] => ))) [2] => array ([TID] => 22 [updated] => 10:21:33 [Created] => 10:21:33 [typename] => PHP basics [FID] => 17 [toparticle] => array ([0] => array ([atcid] => 12 [title] => smarty generates paging static pages [TID] => 22 [created] => 10:40:08 [Hits] => 1 [atc_guest] =>) [1] => array ([atcid] => 11 [title] => smarty generate static page [TID] => 22 [created] => 10:36:28 [Hits] => 1 [atc_guest] => )))))) when using a template: You can use the following syntax: {section name = List loop = $ atcs}} <! -- Level 1 category --> <Div align = "Left" >{{$ atcs [LIST]. typename }}</div>
{Assign Var = smalltype value = $ atcs [LIST]. smalltype }}<! -- Level 2 category -- >{{ section name = list1 loop = $ smalltype }}< Div align = "Left"> & nbsp; -- >{{ $ smalltype [list1]. typename }}</div>
{Assign Var = topatc value = $ smalltype [list1]. toparticle }}<! -- Several articles in Level 2 Classification -- >{{ section name = list2 loop = $ topatc }}< Div align = "Left"> & nbsp; ------- {$ topatc [list2]. title }}----- {$ topatc [list2]. created }}</Div >{{/section }}
{{/section}}
The result is as follows:
System Problems Html/CSS/Ajax --> htmlbook -------- article test ------- 10:27:13 PHP programming --> fleaphp framework --> PHP code library -------- special post ------- 09:59:25 --> PHP basics -------- smarty generate pagination Static Page ------- 10:40:08 -------- smarty generate static page ------- 10:36:28
Foreach traverses multi-dimensional arrays
The array looks like:
Array (
'Table1' => array (
'Seq '=> KKK
'Title' => B
)
)
{foreach key=key item=item from=$dataList name=foo}            {section name=l loop=$item}            <li><a href="#{$item[l].SEQ}" >·{$item[l].TITLE}</a></li>        {/section}{/foreach}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.