ColdFusion Home

How to create a ColdFusion array

Define when you create the array

< cfset firstnameImplicit=["Tony","Justin","Jon"]>

Here I am Going to do a cfdump of the array so far

< cfdump var=#firstnameImplicit#>

array
1 Tony
2 Justin
3 Jon

Create it 1 iteam at a time or insert extra later on

< cfset array_name = ArrayNew(1)>
< cfset ArrayAppend(array_name, "What are ColdFusion arrays")>
< cfset ArrayAppend(array_name, "How to create a ColdFusion array?")>
< cfset ArrayAppend(array_name, "What are two dimensional arrays?")>

The first step where you name the array has a (1) in it. This has to be either a 1, 2, or 3.
Teh number referances the dimenstions of the array.

To add an item to the end of an array, use the ArrayAppend() function
To add an item to the beginning of the array, use the ArrayPrepend() function:
To insert an item in a specific position in the array, use the ArrayInsertAt() function (in this case, the new value is added before position 3):

< cfset ArrayInsertAt(array_name, 3, "How to modify an array?")>


Here I am Going to do a cfdump of the array so far
array
1 What are ColdFusion arrays
2 How to create a ColdFusion array?
3 How to modify an array?
4 What are two dimensional arrays?

Here we are going to call a single element of teh array

< cfoutput>#array_name[2]#</cfoutput>

#array_name[2]# = How to create a ColdFusion array?

Creating a multi-demensinal array

I pulled this example from another website

< cfset myarray=ArrayNew(1)>
< cfset myotherarray=ArrayNew(2)>
< cfset biggerarray=ArrayNew(3)>

< cfset biggerarray[1][1][1]=myarray>
< cfset biggerarray[1][1][1][10]=3>
< cfset biggerarray[2][1][1]=myotherarray>
< cfset biggerarray[2][1][1][4][2]="five deep">

< cfset biggestarray=ArrayNew(3)>
< cfset biggestarray[3][1][1]=biggerarray>
< cfset biggestarray[3][1][1][2][3][1]="This is complex">
< cfset myarray[3]="Can you see me">

< cfdump var=#biggestarray#>

< cfdump var=#myarray#>



array
1
array [empty]
2
array [empty]
3
array
1
array
1
array
1
array
1
array
1
array
1 [undefined array element]
2 [undefined array element]
3 [undefined array element]
4 [undefined array element]
5 [undefined array element]
6 [undefined array element]
7 [undefined array element]
8 [undefined array element]
9 [undefined array element]
10 3
2
array
1
array
1
array
1
array [empty]
2
array [empty]
3
array [empty]
4
array
1 [undefined array element]
2 five deep
2
array [empty]
3
array
1 This is complex

array
1 [undefined array element]
2 [undefined array element]
3 Can you see me

Creating my own multidimensional array

Here I am creating MY own that I can use at other locations.
Creating the first array and dumping it out.

<cfset first = [ '< tag >' , 'Description' , 'Link' ] />
<cfdump var = #first# label = 'Single Dimensional array' >

Single Dimensional array - array
1 &lt; tag &gt;
2 Description
3 Link
Creating the second array

<cfset second = [ '< tag >' , 'Description' , 'Link' ] />
<cfdump var = #second# label = 'Single Dimensional array' >

Single Dimensional array - array
1 < tag >
2 Description
3 Link
Creating the third array

<cfset third = [ 'XXX' , 'XXX' , 'XXX' ] />
<cfdump var = #third# label = 'Single Dimensional array' >

Single Dimensional array - array
1 XXX
2 XXX
3 XXX
Creating the multidimensional array

< cfset TestArray=ArrayNew(2)>
< cfset TestArray[1]=first >
< cfset TestArray[1][1] = 'Changed on the fly' >
< cfset TestArray[2]=second >
< cfset TestArray[3]=THIRD >

< cfdump var = #TestArray# label = 'Two Dimensional array' >

Two Dimensional array - array
1
Two Dimensional array - array
1 Changed on the fly
2 Description
3 Link
2
Two Dimensional array - array
1 < tag >
2 Description
3 Link
3
Two Dimensional array - array
1 XXX
2 XXX
3 XXX