106
Multidimensional Arrays
ColdFusion supports dynamic multidimensional arrays. When you declare an array
with the ArrayNew function, you can specify up to three dimensions. However, you can
increase an array's dimensions by nesting arrays as array elements:
<CFSET myarray=ArrayNew(1)>
<CFSET myotherarray=ArrayNew(2)>
<CFSET biggerarray=ArrayNew(3)>
<CFSET biggerarray[1][1][1]=myarray>
<CFSET biggerarray[1][1][1][10]= some_value >
<CFSET biggerarray[2][1][1]=myotherarray>
<CFSET biggerarray[2][1][1][4][2]= some_value >
<CFSET biggestarray=ArrayNew(3)>
<CFSET biggestarray[3][1][1]=biggerarray>
<CFSET biggestarray[2][1][1][2][3][1]= some_value >
Basic Array Techniques
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:
Developing Web Applications with ColdFusion
Need help?
Do you have a question about the COLDFUSION 4.5-DEVELOPING WEB and is the answer not in the manual?