Search code examples
arrayssmartyoffsetprestashop-1.6

Smarty get offset of array


I have some trouble in Prestashop 1.6 using Smarty.

I have an array, but its offset are not reset for each product.

So for the first product, with attribute it has offset 1,2,3,4 Then for the next product it has offset 5,6,7,8 etc.

I have that kind of array

$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)

I try to go through this array but it does not work when I put empty offset (it is inside a foreach)

{$combinations[]['quantity']}

How can I tell him to go through the first iteration, and then in the second automatically ?

This return to me the following errors.

Fatal error: Cannot use [] for reading in /htdocs/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 584

I can not tell it which offset to use, because for each product it goes up and is not reset to 0.


Solution

  • Here's how to do it, current return the first value of an array

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