Home page   WebCheatSheet.com


ASP: Multidimensional Arrays


Arrays do not have to be a simple list of keys and values; each location in the array can hold another array. This way, you can create a multi-dimensional array. The reason you may use this type of array is when you have records of each item.  For example, car, year, price,... and you want display one record at a time.

The most commonly used are two-dimensional arrays. You can think of a two-dimensional array as a matrix, or grid, with width and height or rows and columns. Here is how you could define two-dimensional array and display the array values on the web page:

<%@ LANGUAGE="VBSCRIPT" %>
<%
Dim arrCars(2,4) 

'arrCars(col,row)
arrCars(0,0) = "BMW"
arrCars(1,0) = "2004"
arrCars(2,0) = "45.000"
arrCars(0,1) = "Mercedes"
arrCars(1,1) = "2003"
arrCars(2,1) = "57.000"
arrCars(0,2) = "Audi"
arrCars(1,2) = "2000"
arrCars(2,2) = "26.000"
arrCars(0,3) = "Bentley"
arrCars(1,3) = "2005"
arrCars(2,3) = "100.00"
arrCars(0,4) = "Mini"
arrCars(1,4) = "2004"
arrCars(2,4) = "19.00"

Response.Write(" <TABLE border=0>")
Response.Write("<TR><TD>Row</TD> <TD>Car</TD>")
Response.Write("<TD>Year</TD><TD>Price</TD></TR>")

'The UBound function will return the 'index' of the highest element in an array.
For i = 0 to UBound(arrCars, 2)
Response.Write("<TR><TD>#" & i & "</TD>")
Response.Write("<TD>" & arrCars(0,i) & "</TD>")
Response.Write("<TD>" & arrCars(1,i) & "</TD>")
Response.Write("<TD>" & arrCars(2,i) & "</TD></TR>")
Next

Response.Write("</TABLE>")

%>


Copyright © 2005-2007 www.WebCheatSheet.com All Rights Reserved.