coldfusion ColdFusion Arrays Creating Arrays


Creating arrays explicitly using ArrayNew()

Declare an array with the ArrayNew function. Specify the number of dimensions as an argument.

  • ArrayNew(dimension) creates an array of 1–3 dimensions.
  • ColdFusion arrays expand dynamically as data is added.
  • ArrayNew() returns an array.


Introduced in ColdFusion MX 6



<!--- One Dimension Array--->
<cfset oneDimensionArray = ArrayNew(1)>

CFScript Note that inside a function you should use var scoping. Earlier versions of CF required var scoping to be the first thing in a function; later versions allow it anywhere in a function.

    oneDimensionArray = ArrayNew(1);

    public void function myFunc() { 
        var oneDimensionArray = ArrayNew(1);

After creating the array, add elements by using the element indexes. The Coldfusion Array index starts from 1:


<cfset oneDimensionArray[1] = 1>
<cfset oneDimensionArray[2] = 'one'>
<cfset oneDimensionArray[3] = '1'>


    oneDimensionArray[1] = 1;
    oneDimensionArray[2] = 'one';
    oneDimensionArray[3] = '1';

Using ArrayAppend()

You can add elements to an array using the function ArrayAppend(array, value).

    ArrayAppend(oneDimensionArray, 1);
    ArrayAppend(oneDimensionArray, 'one');
    ArrayAppend(oneDimensionArray, '1');

Output the array contents using <cfdump>:

<cfdump var="#oneDimensionArray#">


one Dimension Array Dump Result


<!--- Two Dimension Array--->
<cfset twoDimensionArray = ArrayNew(2)>
<cfset twoDimensionArray[1][1] = 1>
<cfset twoDimensionArray[1][2] = 2>
<cfset twoDimensionArray[2][1] = 3>
<cfset twoDimensionArray[2][2] = 4>


    twoDimensionArray = ArrayNew(2);
    twoDimensionArray[1][1] = 1;
    twoDimensionArray[1][2] = 2;
    twoDimensionArray[2][1] = 3;
    twoDimensionArray[2][2] = 4;

Outputting the contents of array using <cfdump>

<cfdump var="#twoDimensionArray#">


Two Dimension Array Dump Result

Each element contains another Array, which will store the values.

Creating 1-D Array Implicitly

When creating an array implicitly, brackets ([]) surround the array contents with comma separators.

<cfset oneDimensionArrayImplicit = [ 1 ,'one','1' ]>

This statement is equivalent to the four statements used to create the above oneDimensionArray. The result are the same when using:

<cfdump var="#oneDimensionArrayImplicit#">

Create 2-D Array Implicitly

<cfset twoDimensionArrayImplicit = [[ 1 , 2 ],[ 3 , 4 ],[ 5 , 6 ]]>


<cfset firstElement = ["1", "2"]> 
<cfset secondElement= ["3", "4"]> 
<cfset thirdElement = ["5", "6"]> 
<cfset twoDimensionArrayImplicit = [firstElement , secondElement, thirdElement]>

Outputting the content using

<cfdump var="#twoDimensionArrayImplicit#">

Implicit two Dimension Array

Alternative Explicit Declaration

Also you can declare 1 Dimension Array as

<cfset oneDimensionArray = []>

    oneDimensionArray = [];

This declaration is same as that of using ArrayNew(1).

But if you try declaring 2 Dimension Array as

<cfset twoDimensionArray =[][]>    //Invalid CFML construct

an error will occur while processing this request.

Following statement will process the request:

<cfset twoDimensionArray =[]>

but variable twoDimensionArray will not actually an Array within Array (or 2-Dimension Array). It actually contains structure within Array.

<cfset twoDimensionArray =[]>
<cfset twoDimensionArray[1][1] = 1>
<cfset twoDimensionArray[1][2] = 2>
<cfset twoDimensionArray[2][1] = 3>
<cfset twoDimensionArray[2][2] = 4>

<cfdump var="#twoDimensionArray#">


two Dimension Array using [] explicit declaration