如何用 smarty foreach 打开一个数组?

How to open an array with smarty foreach?

我的数组如下所示:

Array ( 
    [seller1] => Array ( 
        [0] => Array ( 
            [reference] => seller1 
            [name] => Lime verder 
            [unit_price] => 44,00 $ 
            [price] => 26,40 $ 
            [quantity] => 0.6 
            [customization] => Array ( ) 
        ) 
        [1] => Array ( 
            [reference] => seller1 
            [name] => Banane 
            [unit_price] => 12,00 $ 
            [price] => 12,00 $ 
            [quantity] => 1 
            [customization] => Array ( ) 
        ) 
    ) 
    [seller2] => Array ( 
        [0] => Array ( 
            [reference] => seller2 
            [name] => qiwi 
            [unit_price] => 33,00 $ 
            [price] => 16,50 $ 
            [quantity] => 0.5 
            [customization] => Array ( ) 
        ) 
        [1] => Array ( 
            [reference] => seller2 
            [name] => Banane 
            [unit_price] => 25,00 $ 
            [price] => 12,50 $ 
            [quantity] => 0.5 
            [customization] => Array ( ) 
        ) 
        [2] => Array ( 
            [reference] => seller2 
            [name] => Porotocale 
            [unit_price] => 32,00 $ 
            [price] => 48,00 $ 
            [quantity] => 1.5 
            [customization] => Array ( ) 
        )
    ) 
    [seller3] => Array ( 
        [0] => Array ( 
            [reference] => seller3 
            [name] => portocale 
            [unit_price] => 21,00 $ 
            [price] => 21,00 $ 
            [quantity] => 1 
            [customization] => Array ( ) 
        ) 
        [1] => Array ( 
            [reference] => seller3 
            [name] => Babana 
            [unit_price] => 26,00 $ 
            [price] => 52,00 $ 
            [quantity] => 2 
            [customization] => Array ( ) 
        ) 
    ) 
) 1

在 smarty 中我尝试使用:

{foreach from=$sellerProducts key=$seller item=$products}
    <tr>
      <td>{$seller}</td>
      <td>
        <ol>
        {foreach from=$products item=$product}
          <li>{$product.name}</li>
        {/foreach}
        </ol>
      </td>
    </tr>
  {/foreach}

但我得到空行作为输出。在同一个 *tpl 文件中,还有另一个 foreach 循环,它看起来与我的不同:

{foreach $list as $product}

是否与smarty版本有关?

版本 2 中的 {foreach} 语法(在版本 3 中仍受支持)使用字符串值作为 key=item= 参数。因此,您必须将 {foreach} 循环更改为:

{foreach from=$sellerProducts key=seller item=products}
...
{/foreach}