Home » Tutorials » ColdFusion » Arrays

Arrays

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

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

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 FunctionsFor an array, I have broken down the important array functions for you below.

ArrayAppendArrayIsDefinedArrayNewArraySum
ArrayAvgArrayIsEmptyArrayPrependArraySwap
ArrayClearArrayLenArrayResize
ArrayDeleteAtArrayMaxArraySetIsArray
ArrayInsertAtArrayMinArraySortListToArray


Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Arrays". After Hours Programming. Accessed on March 18, 2024. https://www.afterhoursprogramming.com/tutorial/coldfusion/arrays-cf/.

  • Stewart, Suzy. "Arrays". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/coldfusion/arrays-cf/. Accessed 18 March, 2024.

  • Stewart, Suzy. Arrays. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/coldfusion/arrays-cf/.



Leave a Comment

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