ColdFusion 9.0 Resources Developing ColdFusion 9 Applications |
Referencing complex structuresWhen a structure contains another structure, you reference the data in the nested structure by extending either object.property or associative array notation. You can even use a mixture of both notations. For example, if structure1 has a key key1 whose value is a structure that has keys struct2key1, struct2key2, and so on, you can use any of the following references to access the data in the first key of the embedded structure: Structure1.key1.Struct2key1 Structure1["key1"].Struct2key1 Structure1.key1["Struct2key1"] Structure1["key1"]["Struct2key1"] The following example shows various ways you can reference the contents of a complex structure: <cfset myArray=ArrayNew(1)> <cfset myArray[1]="2"> <cfset myArray[2]="3"> <cfset myStruct2=StructNew()> <cfset myStruct2.struct2key1="4"> <cfset myStruct2.struct2key2="5"> <cfset myStruct=StructNew()> <cfset myStruct.key1="1"> <cfset myStruct.key2=myArray> <cfset myStruct.key3=myStruct2> <cfdump var=#myStruct#><br> <cfset key1Var="key1"> <cfset key2Var="key2"> <cfset key3Var="key3"> <cfset var2="2"> <cfoutput> Value of the first key<br> #mystruct.key1#<br> #mystruct["key1"]#<br> #mystruct[key1Var]#<br> <br> Value of the second entry in the key2 array<br> #myStruct.key2[2]#<br> #myStruct["key2"][2]#<br> #myStruct[key2Var][2]#<br> #myStruct[key2Var][var2]#<br> <br> Value of the struct2key2 entry in the key3 structure<br> #myStruct.key3.struct2key2#<br> #myStruct["key3"]["struct2key2"]#<br> #myStruct[key3Var]["struct2key2"]#<br> #myStruct.key3["struct2key2"]#<br> #myStruct["key3"].struct2key2#<br> <br> </cfoutput> Reviewing the codeThe following table describes the code:
|