Arrays

An array is an object that stores indexed values. Each value stored in an array is referred to as an element of the array. Each element has an integer assigned to it that marks its position within the array. This number is referred to as the element’s index. Arrays elements can store any ColdFusion datatype including additional arrays.

As previously discussed, ColdFusion supports one-dimensional (think of this as a single column of data or a list), two-dimensional (think of this as a spreadsheet with rows and columns), and three-dimensional (think of this as a cube of data) arrays. Additional dimensions can be created dynamically by nesting multidimensional arrays (creating an array of arrays). ColdFusion arrays differ slightly from traditional arrays found in other programming languages. Whereas traditional arrays are fixed in size, ColdFusion arrays are dynamic. This means that a ColdFusion array can expand or contract as elements are added and removed from the array.

Arrays can store groups of related data such as the contents of a visitor’s shopping cart, student test scores, or historic stock prices. Because array elements can store any ColdFusion datatype, they are ideal for storing complex objects such as an array of query result sets or an array of structures.

Initializing an Array

Initializing an array in ColdFusion is accomplished using the ArrayNew( ) function. This function takes a single argument that determines the number of dimensions for the array (1, 2, ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.