User Guide

Table Of Contents
102 Chapter 5: Using Arrays and Structures
Adding elements to an array
You can add an element to an array by assigning the element a value or by using a ColdFusion
function.
Adding an array element by assignment
You can add elements to an array by defining the value of an array element, as shown in the
following
cfset tag:
<cfset myarray[5]="Test Message">
If an element does not exist at the specified index, ColdFusion creates it. If an element already
exists at the specified index, ColdFusion replaces it with the new value. To prevent existing data
from being overwritten, use the
ArrayInsertAt function, as described in the next section.
If elements with lower-number indexes do not exist, they remain undefined. You must assign
values to undefined array elements before you can use them. For example, the following code
creates an array and an element at index 4. It outputs the contents of element 4, but generates an
error when it tries to output the (nonexistent) element 3.
<cfset myarray=ArrayNew(1)>
<cfset myarray[4]=4>
<cfoutput>
myarray4: #myarray[4]#<br>
myarray3: #myarray[3]#<br>
</cfoutput>
Adding an array element with a function
You can use the following array functions to add data to an array:
<cfset myarray[3]="Can you see
me">
Assign a value to element [3] of myarray.
<cfdump var=#biggestarray#><br>
<cfdump var=#myarray#>
Use cfdump to view the structure of biggestarray and
myarray.
Notice that the "Can you see me" entry appears in myarray,
but not in biggestarray, because biggestarray has a copy of
the original myarray values and is not affected by the change
to myarray.
Function Description
ArrayAppend
Creates a new array element at the end of the array.
ArrayPrepend
Creates a new array element at the beginning of the array.
ArrayInsertAt
Inserts an array element at the specified index position.
Code Description