Not yet rated

Problem

You need to loop over an array and output all the values.

Solution

There are two different ways to loop over arrays in ColdFusion. Using an index loop and accessing the elements of the array by the specified index and specifying the array itself and outputting the specific element in the array.

Detailed explanation

 

Using an index loop:

Looping over the array using an index loop.  ColdFusion arrays start their index at 1 unlike other languages where they start at 0, thus it'spossible to loop from 1 to the length of the array.

<!--- create the array --->

<cfset family = [ "Thadeus", "Hank", "Dean", "Brock" ] />

<cfoutput>

<ul>

<cfloop from="1" to="#ArrayLen( family )#" index="i">

<li>#family[i]#</li>

</cfloop>

</ul>

</cfoutput>

Using an array loop:

When using an array loop, the array is specified and the index contains the value in the array at the current position in the loop.

<!--- create the array --->

<cfset family = [ "Thadeus", "Hank", "Dean", "Brock" ] />

<cfoutput>

<ul>

<cfloop array="#family#" index="i">

<li>#i#</li>

</cfloop>

</ul>

</cfoutput>

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes