# Two-dimensional Arrays

Two-dimensional Arrays

# Two-dimensional Arrays

ColdFusion supports two-dimensional arrays, which are similar to tables or grids. You can think of the outer array as containing the rows and the inner arrays as containing the data cells in those rows. For example, a two-dimensional array called rockbands could contain the names of the bands and some of the songs that they sing. Below is a grid that represents such a two-dimensional array.

Rockband Song1 Song2 Song3
Beatles Love Me Do Hey Jude Helter Skelter
Rolling Stones Waiting on a Friend Angie Yesterday's Papers
Eagles Life in the Fast Lane Hotel California Best of My Love

The following code sample shows how this two-dimensional array is created. Note that the header row is not included.

## Code Sample:

ListsAndArrays/Demos/Arrays-2Dim-create.cfm
```<html>
<title>Rockbands</title>

<body>
<h1>Rockbands</h1>
<cfset rockbands=ArrayNew(2)>
<cfset rockbands[1][1]="Beatles">
<cfset rockbands[1][2]="Love Me Do">
<cfset rockbands[1][3]="Hey Jude">
<cfset rockbands[1][4]="Helter Skelter">

<cfset rockbands[2][1]="Rolling Stones">
<cfset rockbands[2][2]="Waiting on a Friend">
<cfset rockbands[2][3]="Angie">
<cfset rockbands[2][4]="YesterdayÂ’s Papers">

<cfset rockbands[3][1]="Eagles">
<cfset rockbands[3][2]="Life in the Fast Lane">
<cfset rockbands[3][3]="Hotel California">
<cfset rockbands[3][4]="Best of My Love">

</body>
</html>
```

To read an element from a two-dimensional array, you must first identify the index of the "row" and then identify the index of the "column." For example, the song "Angie" is in row 2, column 3, so it is identified as rockbands[2][3].

## Looping through Two-dimensional Arrays

To loop through a two-dimensional array, you need to nest one loop inside of another. The following code will create an HTML table from our two-dimensional array.

## Code Sample:

```---- C O D E   O M I T T E D ----
<table border="1">
<cfloop from="1" to="#ArrayLen(rockbands)#" index="i">
<tr>
<cfoutput>
<cfloop from="1" to="#ArrayLen(rockbands[i])#" index="j">
<td>#rockbands[i][j]#</td>
</cfloop>
</cfoutput>
</tr>
</cfloop>
</table>

</body>
</html>
```