ColdFusion 9.0 Resources |
Referencing array elementsYou reference array elements by enclosing the index with brackets: arrayName[x] where x is the index that you want to reference. In ColdFusion, array indexes are counted starting with position 1, which means that position 1 in the firstname array is referenced as firstname[1]. For 2D arrays, you reference an index by specifying two coordinates: myarray[1][1]. You can use ColdFusion variables and expressions inside the brackets to reference an index, as the following example shows: <cfset myArray=ArrayNew(1)> <cfset myArray[1]="First Array Element"> <cfset myArray[1 + 1]="Second Array" & "Element"> <cfset arrayIndex=3> <cfset arrayElement="Third Array Element"> <cfset myArray[arrayIndex]=arrayElement> <cfset myArray[++arrayIndex]="Fourth Array Element"> <cfdump var=#myArray#> Note: The IsDefined function does not
test the existence of array elements. Instead, place any code that
could try to access an undefined array element in a try block and
use a catch block to handle exceptions that arise if elements do
not exist.
|