1

我坚持使用 smarty 中的这种多维数组处理(更准确地说是 prestashop)。

我在遍历所有元素时遇到了麻烦。

这是我的数组,print_r-ed。

(
    [0] => Array
        (
            [superorder] => 1
            [basket] => Array
                (
                    [0] => Array
                        (
                            [product_name] => iPod shuffle
                            [position_left] => 37
                            [position_top] => 73
                            [order] => 1
                        )

                    [1] => Array
                        (
                            [product_name] => iPod touch
                            [position_left] => 5
                            [position_top] => 66
                            [order] => 2
                        )

                    [2] => Array
                        (
                            [product_name] => iPod Nano
                            [position_left] => 67
                            [position_top] => 49
                            [order] => 3
                        )

                )

        )

    [1] => Array
        (
            [superorder] => 2
            [basket] => Array
                (
                    [0] => Array
                        (
                            [product_name] => iPod shuffle
                            [position_left] => 37
                            [position_top] => 73
                            [order] => 1
                        )

                    [1] => Array
                        (
                            [product_name] => iPod touch
                            [position_left] => 5
                            [position_top] => 66
                            [order] => 2
                        )

                    [2] => Array
                        (
                            [product_name] => iPod Nano
                            [position_left] => 67
                            [position_top] => 49
                            [order] => 3
                        )

                )

        )

)

这就是我正在尝试使用的,但失败得很惨:

{foreach from=$all_baskets name=outer key=superorder item=basket}

    <div superorder="{$basket.superorder}" >


        {foreach from=$basket name=inner key=i item=basket_image}

            <div product_name="{$basket_image.product_name}" order="{$basket_image.order}" style="left:{$basket_image.position_left}px; top:{$basket_image.position_top}px"></div>

        {/foreach}

    </div>

{/foreach}

我是 smarty 的新手,我知道我做错了什么,所以我希望你能帮助我。谢谢!

4

1 回答 1

2

没关系,伙计们,我明白了。我没有注意到子数组 [basket] 应该在第二个循环级别内调用。这是解决方案:

{foreach from=$all_baskets name=outer key=superorder item=singlebasket}

            <div class="holder active" superorder="{$singlebasket.superorder}" >

                <span {if $cart_image_url!=""}style="background:url('{$cart_image_url}')"{/if} class="basketbg"></span>

                {foreach from=$singlebasket.basket name=inner key=i item=basket_image}

                    <img class="slot slot-{$basket_image.order}" product_name="{$basket_image.product_name}" src="{$basket_image.url}" order="{$basket_image.order}" style="left:{$basket_image.position_left}px; top:{$basket_image.position_top}px" />

                {/foreach}

            </div>

        {/foreach}
于 2013-04-02T10:35:16.147 回答