从Smarty数组显示

时间:2012-10-17 11:35:45

标签: php multidimensional-array foreach smarty

我在smarty中遵循了数组类型..

[Main_array] => Array
(
    [splitlist] => 1
    [counter] => 2
    [listNames] => Array
        (
            [0] => Material
            [1] => Color
        )

    [splittedLists] => Array
        (
            [Material_item_1] => Array
                (
                    [White] => Array
                        ( 
                            [image] => /img/thumbnail.gif
                            [imageLink] => /static/white.html
                        )

                    [Black] => Array
                        (

                            [image] => /img/no-image.gif
                            [imageLink] => /static/black.html
                        )
                )

            [Material_item_2] => Array
                (
                    [Red] => Array
                        (
                            [image] => /img/no-image.gif
                            [imageLink] => /static/Red.html
                        )

                    [Yellow] => Array
                        (
                            [image] => /img/no-image.gif
                            [imageLink] => /static/yellow.html
                        )

                )
        )
)

我想要跟随此数组的输出

*  Material
    -  Material_item_1
    -  Material_item_2
*  Color
    -  Nude
    -  Black
    -  Red
    -  Yellow

到目前为止我所做的事情如下:

[{foreach from=$item2.listNames key=subKey1 item=subItem1 name=subLp1}]
   <h2>
       <label>[{$subItem1|replace:"-":" "}]</label>
   </h2>
   <ul style="margin-left:20px;display:block;">
      [{foreach from=$item2.splittedLists key=subKey2 item=subItem2 name=subLp2}]
         <li style="float:none;"><strong>[{$subKey2|replace:"-":" "}]</strong></li>
      [{/foreach}]
   </ul>
[{/foreach}]

我得到......

*  Material
    -   Material_item_1
    -   Material_item_1
    -   Material_item_2
    -   Material_item_2
*  Color
    -   Material_item_1
    -   Material_item_1
    -   Material_item_2
    -   Material_item_2

我正在使用PHP - OXID&amp; Smarty的。我应该在这里提到的一件事是[splittedLists]的元素可能大于或小于2.但是对于2元素的逻辑也是受欢迎的。

更新:我已经和客户谈过了,所以,现在任何有关结构变化的建议都是受欢迎的。 Plz帮助

UPDATE2:以下是PHP数组。

$arr = array(
    'Main_array' => array
    (
        'splitlist' => 1,
        'counter' => 2,
        'listNames' => array
        (
            '0' => "Material",
            '1' => "Color"
        ),
        'splittedLists' => array
        (
            'Material_item_1' => array
            (
                'White' => array
                ( 
                   'image' => "/img/thumbnail.gif",
                   'imageLink' => "/static/white.html"
                ),
                'Black' => array
                (
                   'image' => "/img/no-image.gif",
                   'imageLink' => "/static/black.html"
                 )
            ),

            'Material_item_2' => array
                (
                    'Red' => array
                        (
                            'image' => "/img/no-image.gif",
                            'imageLink' => "/static/Red.html"
                        ),

                    'Yellow' => array
                        (
                            'image' => "/img/no-image.gif",
                            'imageLink' => "/static/yellow.html"
                        )
                )
        )
)
);

1 个答案:

答案 0 :(得分:1)

对于在Smarty 3.1.18中用PHP呈现的数组,它应该如下所示:

{foreach $item2.listNames as $listType}
   <h2>
       <label>{$listType}</label>
   </h2>

   <ul style="margin-left:20px;display:block;">

      {if $listType eq 'Material'}
                {foreach $item2.splittedLists as $k => $v}
                    <li style="float:none;"><strong>{$k}</strong></li>
                {/foreach}
      {elseif $listType eq 'Color'}
               {foreach $item2.splittedLists as $k => $v}
                  {foreach $v as $colour => $versions}
                    <li style="float:none;"><strong>{$colour}</strong></li>
                  {/foreach}
               {/foreach}
      {/if}
   </ul>   
{/foreach}

此输出将为:

 Material

    Material_item_1
    Material_item_2

Color

    White
    Black
    Red
    Yellow

(当然没有&#34; Nude&#34;因为它不在数组中而被显示)