Chapter 9: Handling Complex Data with Structures
107
firstname[1]=Coleman
firstname[2]=Dexter
Adding elements to an array
You can add elements to an array by simply defining the value of an array element:
<CFSET myarray[1]=form.variable>
But you can also employ a number of array functions to add data to an array. You can
use ArrayAppend to create a new array index at the end of the array, ArrayPrepend to
create a new array index at the beginning of the array, and ArrayInsertAt to insert an
array index and data. When you insert an array index with ArrayInsertAt, as with
ArrayDeleteAt, all indexes to the right of the new index are recalculated to reflect the
new index count.
For more information about these array functions, see the CFML Language Reference.
Note
Because ColdFusion arrays are dynamic, if you add or delete an element
from the middle of an array, subsequent index positions all change.
Referencing Elements in Dynamic Arrays
In ColdFusion, array indexes are counted starting with position 1, which means that
position 1 is referenced as
firstname[1]
.
Let’s add to the current firstname array example. For 2D arrays, you reference an index
by specifying two coordinates:
myarray[1][1]
.
<!--- This example adds a 1D array to a 1D array --->
<CFSET firstname=ArrayNew(1)>
<CFSET firstname[1]="Coleman">
<CFSET firstname[2]="Charlie">
<CFSET firstname[3]="Dexter">
<!--- First, declare the array --->
<CFSET fullname=ArrayNew(1)>
<!--- Then, add the firstname array to
index 1 of the fullname array --->
<CFSET fullname[1]=firstname>
<!--- Now we’ll add the last names for symmetry --->
<CFSET fullname[2][1]="Hawkins">
<CFSET fullname[2][2]="Parker">
<CFSET fullname[2][3]="Gordon">
Summary of Contents for COLDFUSION 4.5-DEVELOPING WEB
Page 1: ...Allaire Corporation Developing Web Applications with ColdFusion ColdFusion 4 5...
Page 14: ...xiv Developing Web Applications with ColdFusion...
Page 26: ...xxvi Developing Web Applications with ColdFusion...
Page 34: ...8 Developing Web Applications with ColdFusion...
Page 70: ...44 Developing Web Applications with ColdFusion...
Page 84: ...58 Developing Web Applications with ColdFusion...
Page 114: ...88 Developing Web Applications with ColdFusion...
Page 148: ...122 Developing Web Applications with ColdFusion...
Page 174: ...148 Developing Web Applications with ColdFusion...
Page 208: ...182 Developing Web Applications with ColdFusion...
Page 244: ...218 Developing Web Applications with ColdFusion...
Page 274: ...248 Developing Web Applications with ColdFusion...
Page 288: ...262 Developing Web Applications with ColdFusion...
Page 300: ...274 Developing Web Applications with ColdFusion...
Page 350: ...324 Developing Web Applications with ColdFusion...
Page 362: ...336 Developing Web Applications with ColdFusion...