Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

php smarty loop multidimensional array

I'm using smarty for my site, and I'm trying to loop through an array to print out table rows...

The array looks like this:

Array
(
    [TM98800G] => Array
        (
            [zid] => Array
                (
                    [0] => 90001
                    [1] => 90002
                    [2] => 90003
                    [3] => 90004
                    [4] => 90005
                )

            [count] => Array
                (
                    [0] => 10
                    [1] => 10
                    [2] => 20
                    [3] => 25
                    [4] => 15
                )

        )
    [TM76654G] => Array
        (
            [zid] => Array
                (
                    [0] => 90301
                    [1] => 90302
                    [2] => 90303
                    [3] => 90304
                    [4] => 90305
                )

            [count] => Array
                (
                    [0] => 25
                    [1] => 25
                    [2] => 20
                    [3] => 35
                    [4] => 45
                )

        )
)

I'm trying to loop through this and print out tables:

<h5>TM98800G </h5>

<table>
<tr>
  <td>90001</td>
  <td>10</td>
</tr>

<tr>
  <td>90002</td>
  <td>10</td>
</tr>

<tr>
  <td>90003</td>
  <td>20</td>
</tr>

<tr>
  <td>90004</td>
  <td>25</td>
</tr>

<tr>
  <td>90005</td>
  <td>15</td>
</tr>
</table>

<h5>TM76654G</h5>
<table>
<tr>
  <td>90301</td>
  <td>25</td>
</tr>

<tr>
  <td>90302</td>
  <td>25</td>
</tr>

<tr>
  <td>90303</td>
  <td>20</td>
</tr>

<tr>
  <td>90304</td>
  <td>35</td>
</tr>

<tr>
  <td>90305</td>
  <td>45</td>
</tr>
</table>

I tried nested foreach statements and played with sections, but I can't figure out how to loop through it correctly...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
{foreach from=$array key=header item=table}
<h5>{$header}</h5>

<table>
    {foreach from=$table.zid key=k item=zid}
    <tr>
        <td>{$zid}</td>
        <td>{$table.count.$k}</td>
    </tr>
    {/foreach}
</table>
{/foreach}

Should do it I think.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...