BackUp LevelNext

About Arrays

If you've worked with arrays in the C programming language, or even read about working with them in C, you might regard an array as a tabular structure used to hold data, much like a spreadsheet table with clearly defined limits and dimension. A 2-dimensional (2D) array would be like a simple table; a 3-dimensional array would be like a cube made up of individual cells.

ColdFusion arrays aren't quite that simple because they are dynamic. So in a 2D array, for example, you might have what you could think of as columns of differing lengths based on the data that has been added or removed, whereas in a conventional array, array size is constant and symmetrical.

Creating an array

In ColdFusion, you declare an array by assigning a variable name to the new array as follows:

<CFSET mynewarray=ArrayNew(x)>

where x is the number of dimensions (from 1 to 3) in the array you want to create. You can visualize a one-dimensional (1D) array as a string of cells, like a single row from a table.

Once created, you can add data to the array, in this case using a form variable:

<CFSET mynewarray[3]=Form.emailaddress>

Data in an array is referenced by index number, in the following manner:

#My1DArray[index1]#<BR>
#My2DArray[index1][index2]#<BR>
#My3DArray[index1][index2][index3]#

Array terms

The following terms will help you understand subsequent discussions of ColdFusion arrays:

The syntax my2darray[1][3]="Paul" is the same as saying `My2dArray is a two dimensional array and the value of the array element index [1][3] is "Paul".'

Dynamic arrays

Dynamic arrays expand to accept data you add to them and contract as you remove data from them. This diagram shows the difference between a static 2D array and a ColdFusion dynamic 2D array.

Conventional fixed-size 2D array

ColdFusion dynamic 2D array

A ColdFusion 2D array is actually a 1D array that contains a series of additional 1D arrays. Each of the arrays that make up a column can expand and contract independently of any other column.


BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.