Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

3.4. Looping over an Array

You want to iterate over each element in an array.


Use <cfloop> to iterate over each element in an array.

Pet Sounds Track Listing
  <!---loop over array and display each element value--->
  <cfloop from="1" to="#ArrayLen(aPetSounds)#" index="i">
   Song #i#: #aPetSounds[i]#<br>


This example revisits the aPetSounds array and loops from 1 to the length of the array to extract and display the value of each element. Looping over an array to display its values ensures that you won't reference an index that doesn't exist. This code also shows that you can use ColdFusion expressions and variables to reference indexes within an array—in this case, you use the current index of the loop to retrieve the associated index in the array.


This code introduces the ArrayLen() function. ArrayLen() accepts a single argument: the array in question. Similar to the Len() function, which counts the number of characters in a string, ArrayLen() returns the number of elements in an array.

  • + Share This
  • 🔖 Save To Your Account