Understanding arrays
The array is an interesting variable construct. You can think of an array as a collection of commonly named variables with a numeric index. We will take the last code example and transfer it to arrays. Here we will change the backend in major ways, but the frontend will run completely the same from a user's point of view. If you look closer, you will be able to see that we are now passing a variable from page to page rather than the whole questions as before. Let's check out the code:
<!--- Example: 1_18.cfm ---> <!--- Processing ---> <cfparam name="url.question" default="1"> <cfscript> question = arrayNew(1); question[1] = "What is the speed limit?"; question[2] = "What is a car?"; question[3] = "How much is gas?"; answer = arrayNew(1); answer[1] = "55"; answer[2] = "Depends who you ask!"; answer[3] = "more than before"; </cfscript> <!--- Content ---> <cfoutput> <strong>#question[url.question]#</strong><br /> Answer: #answer[url.question]#<br /><br /> </cfoutput> All Questions <hr /> <cfloop from="1" to="#arrayLen(question)#" index="iQuestion"> <cfoutput> <strong>Q</strong>: <a href="?question=#iQuestion#">#question[iQuestion]#</a><br /> </cfoutput> </cfloop>
Rather than two lists at the top, we now have two array variable constructs. Before you start assigning variables to an array, you need to declare the variable as an array type. Arrays can be multi-dimensional. The truth is this is very rarely done, but you may find some use case now and then. We will focus on a single-dimensional array. If you forget to pass in the number of dimensions when you declare an array, you will get an error when that code runs. So, do remember to declare the number of dimensions.
Note
The maximum number of dimensions in ColdFusion is three. Remember that a large array in multi dimensions can consume huge amounts of computer memory. Think twice before using multi-dimensional arrays for this reason.
You will find a rich collection of array functions built into ColdFusion. You can use arrayDeleteAt()
to remove an item in the middle of an array. There is also an arrayInsertAt()
which does the opposite. There are three things that you need to keep in mind when using arrays:
Don't remove items in the middle of an array list without using the built-in functions. This could create a missing element and create an error when looping through the array collection.
Don't count on an item staying in the same indexed position. It may seem odd that we call the position an index and think the item can move. This is different because unlike the index in a book, arrays are dynamic.
The number of items in an array can change. It is best to use the
arrayLen()
function as we did in the example code in order to know the current length of an array.
Now we will perform a minor rewrite and run the same code as a multi-dimensional array. This is also called an array of structures. Each dimension of the array has structure. This allows for some unique layout of data within your application memory. We will add a CFDump at the end of the code, so the structure created can be presented for view:
<!--- Example: 1_19.cfm ---> <!--- Processing ---> <cfparam name="url.faq" default="1"> <cfscript> faq = arrayNew(1); faq[1] = structNew(); faq[1].question = "What is the speed limit?"; faq[1].answer = "55"; faq[2] = structNew(); faq[2].question = "What is a car?"; faq[2].answer = "Depends who you ask!"; faq[3] = structNew(); faq[3].question = "How much is gas?"; faq[3].answer = "more than before"; </cfscript> <!--- Content ---> <cfoutput> <strong>#faq[url.faq].question#</strong><br /> Answer: #faq[url.faq].answer#<br /><br /> </cfoutput> All Questions <hr /> <cfloop from="1" to="#arrayLen(faq)#" index="iFAQ"> <cfoutput> <strong>Q</strong>: <a href="?faq=#iFAQ#">#faq[iFAQ].question#</a><br /> </cfoutput> </cfloop> <cfdump var="#faq#">
Now we have the same basic information but this time our browser window shows the array of structures feeding the output:
We have only touched the surface of what you can do with structures, arrays, and loops. The masters of ColdFusion are still finding creative uses for them. They are pretty simple to master and very flexible to implement.