24/7 Support: 800.608.6482

Videos

Developer Videos

Videos | Arrays



Video Transcript

If you’re familiar with working with Arrays in other Programming Languages, then Arrays in Miva will feel very familiar. The major difference, is that Arrays in Miva are 1 indexed (instead of 0). In most programming languages, the first index is always 0, so if you have 10 elements, the indexes are going to be 0-9. In Miva, everything is 1 indexed, so the same indexes will be 1-10. That’s probably one of the most important things to remember as it will get you every single time if you’re not paying attention. To target a specific index in an array, you can use brackets and the number of the position you want to target. For example: l.people[1]:code is an array as defined by the brackets, and the 1 is referring to the first index in the array, or the first element that array holds and we’re accessing the code number. One thing that can get confusing when first working with arrays and structures is that they are two different things. Arrays are not structures and structures are not arrays. They are two different concepts. However, they can be used together, which can make it confusing.

If we remember from the video on Structures, a structure is just a collection of data accessed through its numbers. A structure can contain other structures or a structure can even contain arrays. Now a structure contains a : syntax to access different elements within that structure. An array is always going to be two brackets with the number of the index you want to target in the middle. Now we’ll look at some examples here in a second, which will help illustrate this concept, but it’s important to note that structures can contain arrays and arrays can contain structures. So they are two different concepts but they can be mixed together which we’ll see here shortly.

Let’s switch over to our text editor and see some examples. Here I have some example code that creates an array with five different things in it. So, in order to create an array there’s nothing special you need to do. You don’t need to initialize anything, or predeclare that this is an array. You simply create a variable, and you use the bracket syntax to define the array and you put the index of the array within the brackets. So here I have a variable called l.people and the first index I want to save the value of “Rick” and Rick is a string for defining different people. I do the same thing for two through five and give it a different value for different people. So now I have this array, l.people and it has five values for the different people in the array. So let’s say I want to output that array to the page, probably the most common way to do that is using a foreach loop which iterates through all of the elements of the array. However, because we’re going to output this to the page, before we do that I need to make sure that this l.people array is assigned to the l.settings structure, that way I can output the values to this page. I could have also made these l.settings:people here, and if I did that, I wouldn’t need this line down here. Both of them work so really it’s a personal preference of how you want to set up your variables. So now that I have this people array in this l.settings structure, I can then use a foreach loop to iterate through each element within the array. So here I’m defining the array, which is l.settings:people, and by convention you don’t have l.settings here, but it really is referring to that particular array. It’s always l.settings when you don’t have a prefix there. If this was in fact a global array, you would need to prefix it with “global:” like this. However, because I know my array is an l.settings structure, I can just leave it with people. In the iterator I’m creating a person, so here I’ve got &mvt:person; with a line break. So what this should do is iterate through each element of my array and output its value to the page. Let’s see if this works. So here I’m on the storefront page of Miva, and I’m going to come down here to the content section, and paste my code in, save it, go back to the front end and hit refresh. Perfect, exactly what we expect. We have all the names printed out with a line break. Let’s jump back to our text editor and let’s try something else.

Say for example we wanted to count the number of elements that are in the array. Well, Miva provides a built in function called “miva_array_elements.” We’re going to get a lot more in depth into Miva’s built in functions in later videos. But for now, just know that they exist and you can use them like this; where you define a variable (name=”g.count,” you use the function “value=”miva_array_elements” and you pass in the array for it to count and then I’m outputting the global variable to the page. So this should return the number of elements that are in the array. Let’s try this, refresh and we see five here which is exactly what we expect because there are five names in the array it counted those up and returned that value back to the page.

Now a couple minutes ago, I said a structure can contain and array, and an array can contain a structure. These are two different things that can work together to give you very powerful data solutions. So here’s an example: so here we have l.settings, which itself is a structure, and then within that we have our array, so “people” is an array defined by the brackets and the index so all three of these is referring to the first index of that people array, so inside that first index, contains a structure called “info.” So this “info” structure information has three different numbers; name, birthday and address. These will be variables which contain their respective data. So this is a good example of a structure which contains an array, which contains another structure, which references three variables. So say I wanted to output the variable to the page, I can do &mvt:people and then I can specify the index [1]: and then, info, which is my structure, and then the number of the structure I want to target: which is name. So this would output the name for the first person in that people structure. Now typically when working in the page template language within Miva Merchant, you’ll rarely see the brackets being used to output the variable. This is because they are usually output with foreach loops. When you have a foreach loop, you don’t use the bracket syntax because the iterator represents the specific index that you’re looking for. The iterator loops through every single element of the array and it contains that current index. So in that case, you don’t have the bracket syntax to target a specific value. The foreach value handles that for you and you just output the variable.

Arrays are a very powerful part of Miva Merchant and used quite a bit throughout the software for things like the category list, the related product list, the search results list, all those are arrays that have foreach loops that iterate through their results. Knowing the difference between an array and a structure, will also save you a lot of time and headache on trying to figure out where a variable came from or how to output it to a page.



Example Code




Looking for Developer Docs?

We have a whole section for that, including: Developer Training Series, Template Language docs, Module Development tutorials and much, much more.

Head to the Developer Section

This website uses cookies to identify visitors, track visitors to our website, store login session information and to remember your user preferences. By continuing to use this site you agree to our use of cookies. Learn More.

This website uses cookies. By continuing to use this site you agree to our use of cookies. Learn More.

Accept

Copyright © 1997 – 2024 Miva®, Miva Merchant®, MivaPay®, MivaCon®, Camp Miva®, Miva Connect®, Miva, Inc. All Rights Reserved.