122
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
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
2D array. It also uses nested
<cfset my2darray=arraynew(2)>
<cfloop index="loopcount" from=1 to=12>
</cfloop>
<p>The values in my2darray are currently:</p>
<cfloop index="OuterCounter" from="1" to="#ArrayLen(my2darray)#">
</cfloop>
Nesting CFLOOPs for a 3D array
For 3D arrays, you simply nest an additional
array values first. You can try doing it as an exercise.)
<cfloop index="Dim1" from="1" to="#ArrayLen(my3darray)#">
</cfloop>
<cfloop index="loopcount2" from=1 to=2>
<cfset my2darray[loopcount][loopcount2]=(loopcount * loopcount2)>
</cfloop>
<cfloop index="InnerCounter" from="1"
to="#ArrayLen(my2darray[OuterCounter])#">
<cfoutput>
<b>[#OuterCounter#][#InnerCounter#]</b>:
#my2darray[OuterCounter][InnerCounter]#<br>
</cfoutput>
</cfloop>
<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>
Chapter 8 Handling Complex Data with Structures
is sufficient to output data, as in the
cfloop
tags to populate the array:
cfloop
cfloop
s to output data from a
cfloop
. (This example does not set the