0

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 firdst product, with attrivute it has offset 1,2,3,4 Then for the next product it has offest 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 trhough 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 trhough 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 him which offset to use, because for each product it goes up and is not reset to 0.

I would be very gratefull for anykind of help.

Stanislas Piotrowski
  • 2,595
  • 10
  • 40
  • 59

2 Answers2

1

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

{$combination = current($combinations)}
{$combination['quantity']}
unloco
  • 6,928
  • 2
  • 47
  • 58
1

in addition to @UnLoCo answer, if you need these keys 1,2 ... 7,8

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

or

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

Also Smarty docs may help you http://www.smarty.net/docs/en/language.function.foreach.tpl

Serge P
  • 1,863
  • 13
  • 14
  • What I need to find is the first key of a given array with no loop. Is it possible ? – Stanislas Piotrowski Apr 18 '16 at 21:45
  • 1
    http://stackoverflow.com/questions/25448838/get-first-smarty-array-element in your case it will be {assign var=first value = $combinations|@key} {$combinations.$first.content} – Serge P Apr 18 '16 at 21:47