ColdFusion arrays are objects that can store a whole bunch of variables. As I said, arrays are objects; therefore, you can even create properties for them. We won’t get into creating their additional properties simply because we won’t create their properties very often. The length of the array does not have to be specified when you are declaring the variable. You can just keep adding variables or elements to it. This makes them perfect for situations where you need to capture an idea, but don’t have to keep creating variable names and polluting the global namespace with worthless variables.

Adding Elements to an Array

<cfset myVar = "meatloaf">
<cfset myArray = arrayNew(1)>
<cfset myArray[1] = "Pickles">
<cfset myArray[2] = "Meerkats">
<cfset myArray[3] = myVar>
    #myArray[1]# <br/>
    #myArray[2]# <br/>
    #myArray[3]# <br/>

If you have ever used an array, you will realize that they are really standard in ColdFusion. If you haven’t, then keep reading this paragraph. Our array’s name is myArray, which we create the array by using the <cfset myArray = arrayNew(1)>. You might be wondering what the 1 is for. All arrays in ColdFusion start with the index of one, which we specify here. Next, you see myArray[] with a number in those brackets. Those numbers are the order of elements in the array. So when we print our array using the <cfoutput> tags, we see that #myArray[1]# gives us “Pickles”. If you look, you can see that we already set that above in a <cfset> tag.

Array Functions

Adobe has all of the documentation for ColdFusion on their website if you ever need to reference it. ColdFusion Functions For an array, I have broken down the important array functions for you below.

ArrayAppend ArrayIsDefined ArrayNew ArraySum
ArrayAvg ArrayIsEmpty ArrayPrepend ArraySwap
ArrayClear ArrayLen ArrayResize ArrayToList
ArrayDeleteAt ArrayMax ArraySet IsArray
ArrayInsertAt ArrayMin ArraySort ListToArray

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.