Referencing Elements in Dynamic Arrays

In ColdFusion, array indexes are counted starting with position 1, which means that position 1 is referenced as firstname[1].

Let's add to the current firstname array example. For 2D arrays, you reference an index by specifying two coordinates: myarray[1][1].

<!--- This example adds a 1D array to a 1D array --->

<CFSET firstname=ArrayNew(1)>

<CFSET firstname[1]="Coleman">
<CFSET firstname[2]="Charlie">
<CFSET firstname[3]="Dexter">

<!--- First, declare the array --->

<CFSET fullname=ArrayNew(1)>

<!--- Then, add the firstname array to
index 1 of the fullname array --->

<CFSET fullname[1]=firstname>

<!--- Now we'll add the last names for symmetry --->

<CFSET fullname[2][1]="Hawkins">
<CFSET fullname[2][2]="Parker">
<CFSET fullname[2][3]="Gordon">

<CFOUTPUT>
    #fullname[1][1]# #fullname[2][1]#<BR>
    #fullname[1][2]# #fullname[2][2]#<BR>
    #fullname[1][3]# #fullname[2][3]#<BR>
</CFOUTPUT>

Additional referencing methods

You can reference array indexes in the standard way: myarray[x] where x is the index you want to reference. You can also use ColdFusion expressions inside the square brackets to reference an index. The following are valid ways of referencing an array index:

<CFSET myarray[1]=expression>
<CFSET myarray[1 + 1]=expression>
<CFSET myarray[arrayindex]=expression>