Declare an array with the ArrayNew function. Specify the number of dimensions as an argument.
Introduced in ColdFusion MX 6
CFML
<!--- 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.
<cfscript>
oneDimensionArray = ArrayNew(1);
public void function myFunc() {
var oneDimensionArray = ArrayNew(1);
}
</cfscript>
After creating the array, add elements by using the element indexes. The Coldfusion Array index starts from 1:
CFML
<cfset oneDimensionArray[1] = 1>
<cfset oneDimensionArray[2] = 'one'>
<cfset oneDimensionArray[3] = '1'>
CFScript
<cfscript>
oneDimensionArray[1] = 1;
oneDimensionArray[2] = 'one';
oneDimensionArray[3] = '1';
</cfscript>
You can add elements to an array using the function ArrayAppend(array, value)
.
<cfscript>
ArrayAppend(oneDimensionArray, 1);
ArrayAppend(oneDimensionArray, 'one');
ArrayAppend(oneDimensionArray, '1');
</cfscript>
Output the array contents using <cfdump>
:
<cfdump var="#oneDimensionArray#">
Results:
CFML
<!--- 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>
CFScript
<cfscript>
twoDimensionArray = ArrayNew(2);
twoDimensionArray[1][1] = 1;
twoDimensionArray[1][2] = 2;
twoDimensionArray[2][1] = 3;
twoDimensionArray[2][2] = 4;
</cfscript>
Outputting the contents of array using <cfdump>
<cfdump var="#twoDimensionArray#">
Result:
Each element contains another Array, which will store the values.
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#">
<cfset twoDimensionArrayImplicit = [[ 1 , 2 ],[ 3 , 4 ],[ 5 , 6 ]]>
Or:
<cfset firstElement = ["1", "2"]>
<cfset secondElement= ["3", "4"]>
<cfset thirdElement = ["5", "6"]>
<cfset twoDimensionArrayImplicit = [firstElement , secondElement, thirdElement]>
Outputting the content using
<cfdump var="#twoDimensionArrayImplicit#">
Alternative Explicit Declaration
Also you can declare 1 Dimension Array as
<cfset oneDimensionArray = []>
<cfscript>
oneDimensionArray = [];
</cfscript>
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#">
Result: