108
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:
Because ColdFusion arrays are dynamic, if you add or delete an element from the array, any
higher-numbered index values all change. For example, the following code creates a two element
array and displays the array contents. It then uses
ArrayPrepend
to insert a new element at the
beginning of the array and displays the result. The data that was originally in indexes 1 and 2 is
now in indexes 2 and 3.
<!--- Create an array with three elements --->
<cfset myarray=ArrayNew(1)>
<cfset myarray[1]="Original First Element">
<cfset myarray[2]="Original Second Element">
<!--- Use cfdump to display the array structure --->
<cfdump var=#myarray#>
<br>
<!--- Add a new element at the beginning of the array --->
<cfscript>
ArrayPrepend(myarray, "New First Element");
</cfscript>
<!--- Use cfdump to display the new array structure --->
<cfdump var=#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.
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......