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:

The following sections describe these techniques.

Populating an array with the ArraySet function

You can use the ArraySet function to populate a 1D array, or one dimension of a multidimensional array, with some initial value, such as an empty string or 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)

The following example initializes the array myarray, indexes 1 to 100, with an empty string:

ArraySet (myarray, 1, 100, "")

Populating an array with the cfloop tag

The cfloop tag provides a common and very efficient method for populating an array. 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 one-dimensional (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 cfloop tags for a 2D array

The following example shows how to handle nested cfloop tags 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 cfloop tags for a 3D array

For 3D arrays, you simply nest an additional cfloop tag. (This example does not set the array values first to keep the code short.)

<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 with the following syntax to define values for array indexes:

<cfset arrayName[index]=queryColumn[row]>

In the following example, a cfloop tag 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>
  <cfset myarray[CurrentRow][2]=LastName>
  <cfset myarray[CurrentRow][3]=FirstName>
  <cfset myarray[CurrentRow][4]=Email>
</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>

This example uses the query object built-in variable CurrentRow to index the first dimension of the array.

Comments