Skip to content

Working with Arrays#


Arrays in Miva Merchant’s template language function similarly to those in other programming languages, with one notable distinction: they are 1-indexed instead of 0-indexed. This means the first element in an array is accessed using index 1, not 0. Understanding this difference is crucial for effectively working with arrays in Miva.

Syntax and Access#

To access specific elements within an array, you utilize brackets [ ] followed by the index number. For instance:

l.people[1]:code

Here, l.people represents an array, and [1] targets the first element in the array, from which the code value is accessed.

Arrays vs. Structures#

It’s essential to differentiate between arrays and structures in Miva Merchant. While arrays are collections of data accessed by numerical indices, structures are distinct entities accessed through named keys. Arrays and structures can be utilized independently or in conjunction with each other, offering versatile data management capabilities.

Examples#

Array Creation and Iteration#

<mvt:foreach iterator="person" array="l.people">
    &mvt:person;<br>
</mvt:foreach>
This code iterates through the l.people array, outputting each element’s value to the page.

Counting Array Elements#

<mvt:assign name="g.count" value="miva_array_elements(l.people)">
&mvt:g.count;
Utilizing the miva_array_elements function, this code counts the number of elements in the l.people array and outputs the result.

Arrays within Structures#

&mvt:people[1]:info:name;

In this example, people represents an array within the l.settings structure. Each element of the array contains a structure named info, from which the name value is accessed.

Summary#

Understanding how to work with arrays in Miva Merchant enables developers to efficiently manage and manipulate data within their templates. By grasping the nuances between arrays and structures and mastering array manipulation techniques, developers can leverage arrays effectively to build dynamic and responsive storefronts in Miva Merchant.