Looping Over Structures


When looping over a structure using <cfloop>, you have no control over the order of the structure's keys. If order is an important factor, an array should be used instead. To loop over a structure, pass the structure to the <cfloop> collection attribute. The item attribute steps through the keys of the structure with each loop as follows:

 <cfset user=StructNew()> <cfset user["FirstName"]="Ben"> <cfset user["LastName"]="Forta"> <cfset user["age"]="21"> <cfset user["email"]="ben@forta.com"> <cfloop collection="#user#" item="i">  <cfoutput>#i#: #user[i]#<br></cfoutput> </cfloop> 

<cfloop> loops through the keys one at a time. In each iteration, i will contain the key name and can thus be used within brackets to access the values.

Key names are case insensitive and unordered. Never make any assumptions about case or the order in which keys will be returned.

TIP

Keys are rarely printed. Instead, you use keys to access or print values. Also, you rarely loop through a collection to print data, but rather to find a certain key or value. For these reasons, the case and order limitations are of little practical significance.




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net