0

我在 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 和 Smarty。我应该在这里提到的一件事是元素[splittedLists]可能大于或小于 2。但也欢迎 2 元素的逻辑。

更新:我已经和客户谈过了,所以现在也欢迎任何关于结构变化的建议。请帮忙

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"
                        )
                )
        )
)
);
4

1 回答 1

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

(当然没有显示“Nude”,因为它不在数组中)

于 2014-05-10T15:01:25.307 回答