Let's look an example of creating an array, create a file in the Chapter_3
directory we have been using called Listing3_28.cfm
with the following content:
<cfset myArray = ArrayNew(1)> <cfset ArrayAppend(myArray, "FirstItem")> <cfset myArray[2] = "SecondItem"> <cfdump var="#myArray#">
When we run this code we get the following:
In the previous code, we created an array using the ArrayNew(1)
function, and passing in the variable 1 to create a one-dimensional array. Then, we used the function ArrayAppend()
to add the string "First Item" to the array. Then, instead of using ArrayAppend()
again, we just defined the position of the array that we wanted to add another item into. To access a variable inside an array, it is simply a matter of defining which item you want using the square brackets notation, []:
<cfset myArray = ArrayNew(1)> <cfset ArrayAppend(myArray, "FirstItem")> <cfset ArrayAppend(myArray...