To use arrays in ColdFusion, as in other languages, you need to first declare the array, specifying its dimension. Once it's declared, you can add array elements, which you can then reference by index.
As an example, say you declare a one-dimensional 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">
Once you've added these names to the array, it has a length of 3:
<CFSET temp=ArrayLen(firstname)>
<!--- temp=3 --->
If you remove data from an index, the array resizes dynamically:
<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
|