Adding Elements To An Array; Referencing Elements In Dynamic Arrays - MACROMEDIA COLDFUSION 4.5-DEVELOPING WEB Develop Manual

Developing web applications with coldfusion
Table of Contents

Advertisement

Chapter 9: Handling Complex Data with Structures
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

Referencing Elements in Dynamic Arrays

In ColdFusion, array indexes are counted starting with position 1, which means that
position 1 is referenced as
Let's add to the current firstname array example. For 2D arrays, you reference an index
by specifying two coordinates:
<!--- 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">
Because ColdFusion arrays are dynamic, if you add or delete an element
from the middle of an array, subsequent index positions all change.
firstname[1]
myarray[1][1]
.
.
107

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 4.5

Table of Contents