在smarty中迭代多维数组

时间:2013-04-02 10:29:51

标签: php arrays smarty

我坚持使用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}

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

1 个答案:

答案 0 :(得分: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}