Populating Arrays with Data

One-dimensional arrays can store any values, including queries, structures, and other arrays. You can use a number of functions to populate an array with data, including ArraySet, ArrayAppend, ArrayInsertAt, and ArrayPrepend. These functions are useful for adding data to an existing array. In addition, several basic techniques are important to master:

Populating an array with ArraySet

You can use the ArraySet function to populate a 1D array, or one dimension of a multi-dimensional array, with some initial value such as an empty string or 0 (zero). This can be useful if you need to create an array of a certain size, but don't need to add data to it right away. Array indexes need to contain some value, such as an empty string, in order to be referenced.

Use ArraySet to initialize all elements of an array to some value:

ArraySet (arrayname, startrow, endrow, value)

This example initializes the array myarray, indexes 1 to 100, with an empty string.

ArraySet (myarray, 1, 100, "")

Populating an array with CFLOOP

A common and very efficient method for populating an array is by creating a looping structure that adds data to an array based on some condition using CFLOOP.

In the following example, a simple one-dimensional array is populated with the names of the months using a CFLOOP. A second CFLOOP is used to output data in the array to the browser.

<CFSET months=ArrayNew(1)>

<CFLOOP INDEX="loopcount" FROM="1" TO="12">

    <CFSET months[loopcount]=MonthAsString(loopcount)>

</CFLOOP>

<CFLOOP INDEX="loopcount" FROM="1" TO="12">

        <CFOUTPUT>
            #months[loopcount]#<BR>
        </CFOUTPUT>

</CFLOOP>

Using Nested Loops for 2D and 3D Arrays

To output values from 2D and 3D arrays, you need to employ nested loops to return array data. With a 1D array, a single CFLOOP is sufficient to output data, as in the example just above. With arrays of dimension greater than one, you need to maintain separate loop counters for each array level.

Nesting CFLOOPs for a 2D array

The following example shows how to handle nested CFLOOPs to output data from a 2D array:

<P>The values in my2darray are currently:

<CFLOOP INDEX="OuterCounter"
    FROM="1" TO="#ArrayLen(my2darray)#">

    <CFLOOP INDEX="InnerCounter" FROM="1"
        TO="#ArrayLen(my2darray[OuterCounter])#">

    <CFOUTPUT>
        <B>[#OuterCounter#][#InnerCounter#]</B>:
        #my2darray[OuterCounter][InnerCounter]#<BR>
    </CFOUTPUT>

    </CFLOOP>

</CFLOOP>

Nesting CFLOOPs for a 3D array

For 3D arrays, you simply nest an additional CFLOOP:

<P>My3darray's values are currently:

<CFLOOP INDEX="Dim1"
    FROM="1" TO="#ArrayLen(my3darray)#">

    <CFLOOP INDEX="Dim2" 
        FROM="1" TO="#ArrayLen(my3darray[Dim1])#">

        <CFLOOP INDEX="Dim3" FROM="1"
            TO="#ArrayLen(my3darray[Dim1][Dim2])#">

        <CFOUTPUT>
            <B>[#Dim1#][#Dim2#][#Dim3#]</B>:
            #my3darray[Dim1][Dim2][Dim3]#<BR>
        </CFOUTPUT>

        </CFLOOP>

    </CFLOOP>

</CFLOOP>