【问题标题】:Displaying from Smarty array从 Smarty 阵列显示
【发布时间】:2012-10-07 15:46:08
【问题描述】:

我在 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"
                        )
                )
        )
)
);

【问题讨论】:

  • 你能显示数组,而不是 var_dump 吗?
  • @Sibu 这个数组我放在这里使用print_r函数,我觉得够了。
  • 我想执行你的代码,所以如果可以提供数组..也许我会尝试
  • 我想要完整的数组输出.. 以便我可以提供帮助:)
  • @Sibu & Vijay: PHP 数组已添加到... 希望这对回答我的问题有用.. 谢谢好友..

标签: php multidimensional-array foreach smarty


【解决方案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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2016-12-07
    • 1970-01-01
    • 2012-11-16
    • 2014-05-25
    相关资源
    最近更新 更多