Built with
Model Glue 2.0

Visit the Coldbox Version of this Site

Home

Complex Data Types

Lists

Arrays

Structures

Advanced Structures

CFC Functionality (It's a specialized complex data type w/methods)

Request a username using your email

Add Employee
How it works

Show Employees

* For sake of simplistic examples, <cfqueryparam> is not used in any of the examples. Please be sure to use <cfqueryparam> in your production code

Thanks to Jeff Peters for writing his great book, "ColdFusion Lists, Arrays, and Structures"

An array is a complex data type that allows you to store more than one value. We create new arrays in ColdFusion using the ArrayNew() function. It takes one argument - the number of dimensions (A max of 3 dimensions).

ArrayNew(1) creates a new array with one dimension

<cfscript>
loginInfo = arrayNew(1);
loginInfo[1] = "Will";
loginInfo[2] = "Leroy";
</cfscript>

Produces this:

array
1 Will
2 Leroy

An Array with two dimensions

<cfscript>
 loginInfo = arrayNew(2);
 loginInfo[1][1] = "Will";
 loginInfo[1][2] = "Tomlinson";
  loginInfo[2][1] = "Leroy";
  loginInfo[2][2] = "HisLastName";
  loginInfo[3][1] = "Michael";
  loginInfo[3][2] = "Sexton";
</cfscript>

Produces this:

array
1
array
1 Will
2 Tomlinson
2
array
1 Leroy
2 HisLastName
3
array
1 Michael
2 Sexton

Woweee! A cool multi-dimensioned array! Unlimited in depth with this format. Remember that arrays with multiple dimensions are just nested arrays. The inner-most array is the only one that stores your values. *Notice how this array looks no different than the preceding array?

<cfscript>
loginInfo[1] = arrayNew(1);
 loginInfo[1][1] = arrayNew(1);
   loginInfo[1][1][1] = "Will";
   loginInfo[1][1][2] = "Tomlinson";
loginInfo[2] = arrayNew(1);
  loginInfo[2][1] = arrayNew(1);
     loginInfo[2][1][1] = "Leroy";
     loginInfo[2][1][2] = "Brown";
loginInfo[3] = arrayNew(1);
   loginInfo[3][1] = arrayNew(1);
      loginInfo[3][1][1] = "Michael";
      loginInfo[3][1][2] = "Sexton";
</cfscript> 

Produces this:

array
1
array
1 Will
2 Tomlinson
2
array
1 Leroy
2 HisLastName
3
array
1 Michael
2 Sexton

An array with 3 dimensions:

 <cfscript>
loginInfo[1] = arrayNew(1);
  loginInfo[1][1] = arrayNew(1);
   loginInfo[1][1][1] = arrayNew(1);
    loginInfo[1][1][1] = "Will";
    loginInfo[1][1][2] = "Tomlinson";
    loginInfo[1][1][3] = 38;
loginInfo[2] = arrayNew(1);
  loginInfo[2][1] = arrayNew(1);
   loginInfo[2][1][1] = arrayNew(1);
    loginInfo[2][1][1] = "Leroy";
    loginInfo[2][1][2] = "Brown";
    loginInfo[2][1][3] = 35;
loginInfo[3] = arrayNew(1);
  loginInfo[3][1] = arrayNew(1);
   loginInfo[3][1][1] = arrayNew(1);
    loginInfo[3][1][1] = "Michael";
    loginInfo[3][1][2] = "Sexton";
    loginInfo[3][1][3] = 59;
</cfscript> 

Produces this:

array
1
array
1 Will
2 Tomlinson
2
array
1 Leroy
2 HisLastName
3
array
1 Michael
2 Sexton

Holy moly! This one is 6 dimensions! They're unlimited... see? Just remember, data only goes into the last dimension. All other dimensions contain arrays only

<cfscript>
contacts[1] = arrayNew(1);
  contacts[1][1] = arrayNew(1);
    contacts[1][1][1] = arrayNew(1);
      contacts[1][1][1][1] = arrayNew(1);
        contacts[1][1][1][1] = arrayNew(1);
          contacts[1][1][1][1][1] = arrayNew(1);
		   contacts[1][1][1][1][1] = "Will";
		   contacts[1][1][1][1][2] = "Tomlinson";
           contacts[1][1][1][1][3] = 38;
		   contacts[1][1][1][1][4] = "112 N Mendenhall St.";
		   contacts[1][1][1][1][5] = "Greensboro";
		   contacts[1][1][1][1][6] = "NC";
contacts[2] = arrayNew(1);
  contacts[2][1] = arrayNew(1);
    contacts[2][1][1] = arrayNew(1);
      contacts[2][1][1][1] = arrayNew(1);
        contacts[2][1][1][1] = arrayNew(1);
          contacts[2][1][1][1][1] = arrayNew(1);
		   contacts[2][1][1][1][1] = "Leroy";
		   contacts[2][1][1][1][2] = "Brown";
           contacts[2][1][1][1][3] = 35;
		   contacts[2][1][1][1][4] = "123 Some Street";
		   contacts[2][1][1][1][5] = "Greensboro";
		   contacts[2][1][1][1][6] = "NC";
</cfscript>

Produces this:

array
1
array
1
array
1
array
1
array
1 Will
2 Tomlinson
3 38
4 112 N Mendenhall St.
5 Greensboro
6 NC
2
array
1
array
1
array
1
array
1 Leroy
2 Brown
3 35
4 123 Some Street
5 Greensboro
6 NC