For the complete experience, please enable JavaScript in your browser. Thank you!

  • Creative Cloud
  • Photoshop
  • Illustrator
  • InDesign
  • Premiere Pro
  • After Effects
  • Lightroom
  • See all
  • See plans for: businesses photographers students
  • Document Cloud
  • Acrobat DC
  • eSign
  • Stock
  • Elements
  • Marketing Cloud
  • Analytics
  • Audience Manager
  • Campaign
  • Experience Manager
  • Media Optimizer
  • Target
  • See all
  • Acrobat Reader DC
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player
  • All products
  • Creative Cloud
  • Individuals
  • Photographers
  • Students and Teachers
  • Business
  • Schools and Universities
  • Marketing Cloud
  • Document Cloud
  • Stock
  • Elements
  • All products
  • Get Support
    Find answers quickly. Contact us if you need to.
    Start now >
  • Learn the apps
    Get started or learn new ways to work.
    Learn now >
  • Ask the community
    Post questions and get answers from experts.
    Start now >
    • About Us
    • Careers At Adobe
    • Investor Relations
    • Privacy  |  Security
    • Corporate Responsibility
    • Customer Showcase
    • Events
    • Contact Us
News
    • 3/22/2016
      Adobe Summit 2016: Are You An Experience Business?
    • 3/22/2016
      Adobe Announces Cross-Device Co-op to Enable People-Based Marketing
    • 3/22/2016
      Adobe and comScore Advance Digital TV and Ad Measurement
    • 3/22/2016
      Adobe Marketing Cloud Redefines TV Experience
Developing Applications Help / 

Populating arrays with data

Adobe Community Help


Applies to

  • ColdFusion

Contact support

 
By clicking Submit, you accept the Adobe Terms of Use.
 

Array elements can store any values, including queries, structures, and other arrays. You can use assignment statements to populate an array. You can also use several 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, consider using the following techniques:

  • Populating an array with the ArraySet function
  • Populating an array with the cfloop tag
  • Populating an array from a query

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 to create an array of a certain size, without adding data to it right away. One reason to do this is so that you can reference all the array indexes. If you reference 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 - 100, with an empty string:

ArraySet (myarray, 1, 100, "")

Populating an array with the cfloop tag

The cfloop tag provides a common and 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.

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

<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="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, remember the following:

  • You cannot add query data to an array all at once. A looping structure is often required to populate an array from a query.
  • You can reference query column data using array-like syntax. For example, myquery.col_name1 references data in the first row in the col_name column of the myquery query.
  • Inside a cfloop query= loop, you do not have to specify the query name to reference the query variables.
    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.

<cfquery name="test" datasource="cfdocexamples">
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.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy

Choose your region United States (Change)   Products   Downloads   Learn & Support   Company
Choose your region Close

Americas

Europe, Middle East and Africa

Asia Pacific

  • Brasil
  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Cyprus - English
  • Česká republika
  • Danmark
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Greece - English
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Malta - English
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • Southeast Asia (Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam) - English
  • 台灣

Commonwealth of Independent States

  • Includes Armenia, Azerbaijan, Belarus, Georgia, Moldova, Kazakhstan, Kyrgyzstan, Tajikistan, Turkmenistan, Ukraine, Uzbekistan

Copyright © 2016 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

AdChoices