Article

Introduction to Data Structures in ColdFusion

Page: 1 2

Multidimensional Arrays

For those of you who are unfamiliar with arrays, each value stored in an array is called an element. Each element can be almost any type of data: a variable (integer, string, Boolean, etc.), a structure, or another array. When you have an array that has an element containing another array, then you have a multi-dimensional array. Just keep in mind that if you create a multidimensional array, such as a two dimensional array, the first dimension can only contain other arrays, not actual text data (i.e. strings or integers).

For example, let's consider the fruit array we created above. Now, we want to keep track of the colors each fruit comes in. In order to do this, we'll keep track of our colors in a multidimensional array. It'll look like this:

 <cfset colors = ArrayNew(2)>  
 <cfset colors[1][1]  = \\\"green\\\">  
 <cfset colors[1][2] = \\\"red\\\">  
 <cfset colors[1][3] = \\\"yellow\\\">  
 <cfset colors[2][1] = \\\"orange\\\">  
 <cfset colors[3][1] = \\\"yellow\\\">  
 <cfset colors[3][2] = \\\"green\\\">

This may look sort of complicated, but it really isn't. All we're doing is:

  1. defining a two dimensional array
  2. assigning values to the second dimension of each array

So, here's a pop quiz: How many arrays do we have total? The answer: four. There is an array called colors that holds three other arrays, called colors[1], colors[2], and colors[3]. We treat each of the last three arrays just as we did the fruit array, assigning values to the indexes. We can now display the colors for each fruit like this:

<cfloop from=\\\"1\\\" to=\\\"#ArrayLen(colors)#\\\" index=\\\"outer\\\">  
   <cfoutput><b>#outer# = #fruit[outer]#</b></cfoutput>  
   <blockquote>  
     <cfloop from=\\\"1\\\" to=\\\"#ArrayLen(colors[outer])#\\\" index=\\\"inner\\\">  
       <cfoutput>#inner# - #colors[outer][inner]#</cfoutput>  
     </cfloop>  
   </blockquote>  
 </cfloop>

Here, what we're doing is looping over the top level array (the first dimension) and displaying the fruit name (from the fruit array). Warning: it's not a good idea to do this. Fruit may not have as many dimensions as colors does. This is for demonstrational purposes only.

Real Life

Unless you're creating a very simple e-fruit stand, the above examples don't give you much to go on when you try to use arrays in real life. Here's an example of how I've use arrays on some occasions.

Sometimes it can get pretty tedious trying to create dropdown boxes, especially for things like the months of the year or the days of the week. Here's a simple way to eliminate the monotony of the task. First, we'll create and populate our array.

<cfparam name="selectmonth" default="1">  
<cfset months = ArrayNew(1)>  
<cfloop from="1" to="#DateFormat("12/31/2003", "mm")#" index="j">  
 <cfset months[j] = DateFormat("#j#/1/2003", "mmmm")>  
</cfloop>

The above code ensures that January will be the selected month. You could use a URL or Form variable for this, depending on your application. Then, we create a loop starting at 1 and going to 12. Notice, we use the last day of the year and Put it into DateFormat() to get just the month number, or 12. We then assign each index from 1 to 12 with the name of the month. So, when we call DateFormat("5/1/2003", "mmmm"), it gets the date May 1st 2003 and then just pulls out the month name. Now, we'll display our select box:

<select name="month">  
 <cfloop from="1" to="#ArrayLen(months)#" index="i">  
   <cfif selectmonth eq i>  
     <option value="#i#" selected>#months[i]#</option>  
   <cfelse>  
     <option value="#i#">#months[i]#</option>  
   </cfif>  
 </cfloop>  
</select>

This loops from the beginning index of the array to the ending index to create the drop down box. It checks to see if the current month has been selected and marks it as such if necessary. It's that easy! You can simply copy and paste the code to create the drop down box wherever you need it.

That's it for our overview of using arrays of all sorts in ColdFusion. These handy data structures are essential in nearly every language you'll ever encounter, so get acquainted with them very well. You never know when you might need them!

In our next article, we'll be discussing ColdFusion structures (or structs) in depth, so don't miss it!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: