简体   繁体   English

Smarty嵌套的foreach循环问题

[英]Smarty Nested foreach loop question

I am having trouble getting the below array to display properly in a smarty template file. 我无法让以下数组正确显示在smarty模板文件中。 If anyone can help point me in the right direction I would be most thankful. 如果有人能帮助我指出正确的方向,我将非常感激。

my template code looks like this: 我的模板代码如下所示:

          {foreach from=$trackingGroups item=gender key=k item=v}
          {assign var=tc value="`$v.id`"}    
          {$v.title} ({$v.productCount})<br />

{foreach item=department from=$trackingGroups.$tc item=v2} {$v2.title} {foreach item =部门来自= $ trackingGroups。$ tc item = v2} {$ v2.title}
{/foreach} {/foreach} {/ foreach} {/ foreach}

but the page displays like this: 但是页面显示如下:

          Women (3)<br />
           W<br />
                    7<br />
                    3<br />
                    Footwear<br />
                    Accessories<br />

          Men (2)<br />
           M<br />
                    7<br />
                    2<br />
                    Footwear<br />




Array

( [71] => Array ( [title] => Women [id] => 71 [productCount] => 3 [171] => Array ( [id] => 171 [title] => Footwear [productCount] => 2 [74] => Array ( [id] => 74 [title] => Boots [productCount] => 2 ) ([71] =>数组([title] =>女装[id] => 71 [productCount] => 3 [171] => Array([id] => 171 [title] =>鞋类[productCount] => 2 [74] =>数组([id] => 74 [title] => Boots [productCount] => 2)

            )

        [172] => Array
            (
                [id] => 172
                [title] => Accessories
                [productCount] => 1
                [74] => Array
                    (
                        [id] => 74
                        [title] => Boots
                        [productCount] => 1
                    )

            )

    )

[72] => Array
    (
        [title] => Men
        [id] => 72
        [productCount] => 2
        [171] => Array
            (
                [id] => 171
                [title] => Footwear
                [productCount] => 2
                [74] => Array
                    (
                        [id] => 74
                        [title] => Boots
                        [productCount] => 2
                    )

            )

    )

)

When you are looping in your second foreach you are looping through all the elements, not just the title keys. 当您遍历第二个foreach时,您遍历了所有元素,而不仅仅是title键。 For example: 例如:

{foreach item=department from=$trackingGroups.72 item=v2} 
   {$v2.title}
{/foreach}

in this case $v2 will take the following values: 在这种情况下,$ v2将采用以下值:

[title] => Men
[id] => 72 
[productCount] => 2 
[171] => Array 
 ( 
                [id] => 171 
                [title] => Footwear 
                [productCount] => 2 
                [74] => Array 
                    ( 
                        [id] => 74 
                        [title] => Boots 
                        [productCount] => 2 
                    ) 

 ) 

The first, second and third value will be only a string. 第一个,第二个和第三个值将只是一个字符串。 Since it is not an array, $v2.title will return the "title"'th element (the first one), returning the first character: M,7,2 The fourth element is an array with the "title" key, and it is returned correctly: Footwear. 由于它不是数组,因此$ v2.title将返回第“ title”个元素(第一个元素),并返回第一个字符:M,7,2第四个元素是带有“ title”键的数组,并且正确返回:鞋类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM