Announcement

Collapse
No announcement yet.

Using a Variable as an Array Index Number

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Using a Variable as an Array Index Number

    Hello,

    Is it possible to use a variable as the index number or an array?

    Something like this:

    Code:
    <mvt:assign name="g.counter" value="0" /> <----------- CREATE A VARIABLE NAMED 'COUNTER'
    
    <mvt:foreach iterator="sub_product" array="sub_products">
    
         <mvt:assign name="g.counter" value="g.counter + 1" /> <----------- INCREASE THE VARIABLE BY 1, WITH EACH LOOP ITERATION
        
         <mvt:assign name="l.tracker[g.counter]" value="l.settings:sub_product:code" /> <----------- USE THE CURRENT 'COUNTER' VALUE AS THE ARRAY INDEX
    
    </mvt:foreach>
    Every which way I try this, I get the following error: "Array Index Must Be a Positive Integer"

    Any help would be appreciated!
    Thanks!
    Last edited by allclear; 04-08-16, 10:44 AM.

    #2
    Hello,

    Try this:

    Code:
    <mvt:foreach iterator="sub_product" array="sub_products">
        <mvt:assign name="l.insert" value="miva_array_insert( l.tracker, l.settings:sub_product:code, -1 )" />
    </mvt:foreach>

    http://www.mivascript.com/item/syste...ay_insert.html
    The -1 means it will insert it at the end of your array.
    Last edited by tguefen; 04-08-16, 11:14 AM.

    Comment


      #3
      That worked perfectly. Thank you!

      Comment

      Working...
      X