Populating Arrays with Data

Array elements 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 particular you should master the following basic techniques:

Populating an array with ArraySet

You can use the ArraySet function to populate a 1D, or one dimension of a multidimensional 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 do not need to add data to it right away. One reason to do this is so that you can refer to all the array indexes. If you refer to an array index that does not contain some value, such as an empty string, you get an error.

The ArraySet function has the following form:

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.

The following example uses a cfloop tag and the MonthAsString function to populate a simple 1D array with the names of the months. A second cfloop outputs 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 must employ nested loops to return array data. With a 1D array, a single cfloop is sufficient to output data, as in the previous example. 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. It also uses nested cfloop tags to populate the array:

<cfset my2darray=arraynew(2)>

<cfloop index="loopcount" from=1 to=12>

  <cfloop index="loopcount2" from=1 to=2>

    <cfset my2darray[loopcount][loopcount2]=(loopcount * loopcount2)>

  </cfloop>

</cfloop>



<p>The values in my2darray are currently:</p>



<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. (This example does not set the array values first. You can try doing it as an exercise.)

<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>

Populating an array from a query

When populating an array from a query, keep the following things in mind:

You can use a cfset tag to define values for array indexes, as in the following example:

<cfset arrayname[x]=column[row]>

In the following example, a cfloop places four columns of data from a sample data source into an array, "myarray."

<!--- Do the query --->

<cfquery name="test" datasource="cfsnippets">

  SELECT Emp_ID, LastName, FirstName, Email

  FROM Employees

</cfquery>



<!--- Declare the array --->

<cfset myarray=arraynew(2)>



<!--- Populate the array row by row --->

<cfloop query="test">

  <cfset myarray[currentrow][1]=Emp_ID[currentrow]>

  <cfset myarray[currentrow][2]=LastName[currentrow]>

  <cfset myarray[currentrow][3]=FirstName[currentrow]>

  <cfset myarray[currentrow][4]=Email[currentrow]>

</cfloop>



<!--- Now, create a loop to output the array contents --->

<cfset total_records=test.recordcount>

<cfloop index="Counter" from=1 to="#Total_Records#">

  <cfoutput>

    ID: #MyArray[Counter][1]#,

    LASTNAME: #MyArray[Counter][2]#,

    FIRSTNAME: #MyArray[Counter][3]#,

    EMAIL: #MyArray[Counter][4]# <br>

  </cfoutput>

</cfloop>



Banner.Novgorod.Ru