Smarty 获取数组的偏移量

问题描述 投票:0回答:2

我在 Prestashop 1.6 中使用 Smarty 时遇到一些问题。

我有一个数组,但它的偏移量不会为每个产品重置。

因此,对于第一个产品,其属性的偏移量为 1,2,3,4 然后,对于下一个产品,其属性的偏移量为 5,6,7,8 等。

我有那种数组

$combinations   Smarty_Variable Object (3)
->value = Array (4)
  5 => Array (14)
    attributes_values => Array (1)
      1 => "S"
    attributes => Array (1)
      0 => 1
    price => 0
    specific_price => Array (0)
    ecotax => 0
    weight => 0
    quantity => 20
    reference => ""
    unit_impact => 0
    minimal_quantity => "1"
    date_formatted => ""
    available_date => ""
    id_image => -1
    list => "'1'"
  6 => Array (14)

我尝试遍历这个数组,但当我放置空偏移量(它位于 foreach 内部)时它不起作用

{$combinations[]['quantity']}

我怎样才能告诉他自动进行第一次迭代,然后自动进行第二次迭代?

这返回给我以下错误。

致命错误:无法使用 [] 读取 /htdocs/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) :第 584 行的 eval() 代码

我无法告诉它使用哪个偏移量,因为对于每个产品,它都会上升并且不会重置为 0。

arrays smarty offset prestashop-1.6
2个回答
1
投票

具体操作方法如下,

current
返回数组的第一个值

{$combination = current($combinations)}
{$combination['quantity']}

1
投票

除了@UnLoCo答案之外,如果您需要这些键1,2 ... 7,8

{foreach from=$array key=key item=value}
    {$key} => {$value}
{/foreach}

{foreach $array $key=>$value} {* like PHP style *}
        {$key} => {$value}
{/foreach}

Smarty 文档也可以帮助您 http://www.smarty.net/docs/en/language.function.foreach.tpl

© www.soinside.com 2019 - 2024. All rights reserved.