User Guide
118 Chapter 8 Handling Complex Data with Structures
Basic Array Techniques
To use arrays in ColdFusion, as in other languages, you need to first declare the
array, specifying its dimension. Once it is declared, you can add array elements,
which you can then reference by index.
As an example, suppose you declare a 1D array called "firstname":
<cfset firstname=ArrayNew(1)>
At first, the array firstname holds no data and is of an unspecified length. Now you
want to add data to the array:
<cfset firstname[1]="Coleman">
<cfset firstname[2]="Charlie">
<cfset firstname[3]="Dexter">
After you add these names to the array, it has a length of 3:
<cfset temp=ArrayLen(firstname)>
<!--- temp=3 --->
If you remove data from an array, the array resizes dynamically. Use the
ArrayDeleteAt function to delete data from the array at a particular index, rather
than set the data value to 0 or the empty string:
<cfset temp=ArrayDeleteAt(firstname, 2)>
<!--- "Charlie" has been removed from the array --->
<cfoutput>
The firstname array is #ArrayLen(firstname)#
indexes in length
</cfoutput>
<!--- Now the array has a length of 2, not 3 --->
The array now contains:
firstname[1]=Coleman
firstname[2]=Dexter
Creating an array
In ColdFusion, you declare an array by assigning a variable name to the new array as
follows:
<cfset mynewarray=ArrayNew(x)>
where x is the number of dimensions (from 1 to 3) in the array that you want to
create.
Once created, you can add data to the array, in this case using a form variable:
<cfset mynewarray[4]=Form.emailaddress>