Using Nested Loops For 2D And 3D Arrays - MACROMEDIA COLDFUSION 4.5-DEVELOPING WEB Develop Manual

Developing web applications with coldfusion
Table of Contents

Advertisement

Chapter 9: Handling Complex Data with Structures
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">
</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"
<CFOUTPUT>
</CFOUTPUT>
</CFLOOP>
</CFLOOP>
Nesting CFLOOPs for a 3D array
For 3D arrays, you simply nest an additional CFLOOP:
<CFOUTPUT>
#months[loopcount]#<BR>
</CFOUTPUT>
TO="#ArrayLen(my2darray[OuterCounter])#">
<B>[#OuterCounter#][#InnerCounter#]</B>:
#my2darray[OuterCounter][InnerCounter]#<BR>
109

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 4.5

Table of Contents