Structures

A structure is a ColdFusion datatype that allows you to store and manipulate key/value pairs. Structures are similar to one-dimensional arrays, except each element is referenced via an alphanumeric “key” (string) as opposed to a numeric index within the array. Structures are also commonly known as associative arrays or hashes. Structures offer an advantage over arrays in certain situations, by allowing you to reference groups of related information by key as opposed to by numeric index. Structures are useful for storing sets of related data in a single variable. For example, you can easily store an employee’s contact information in a structure called Employee. That structure can contain keys such as Name, Address, City, State, and Zip. Additionally, a number of ColdFusion variable scopes are accessible as structures. For example, you can access all form variables available to a page by referencing a structure named form. A similar structure is also available for most other variable scopes.

Creating a Structure

Structures are created using the StructNew( ) function. Unlike with arrays, you don’t have to pass any information to this function. To create a new structure called Employee, you use the following code:

<cfset Employee = StructNew( )>

Just like arrays, structures can also be created using the cfparam tag:

<cfparam name="Employee" type="Struct" default="#StructNew( )#">

Be sure to wrap the StructNew( ) function with hash marks to avoid having ColdFusion treat the ...

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.