<!-- JavaScript:Arrays code snippet 8. -->
<!-- Creating a multi-dimensional array: two dimensions example
and example of nested 'for loops' to access data. -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Code source: http://www.justfigures.co.uk/ -->
<!-- A resource for web developers using XHTML, CSS, JavaScript, PHP -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View code snippet page</title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
/* create two arrays to hold related data */
// note that they are the same length
var myArray1=new Array("Potatoes",500,100);
var myArray2=new Array("Rice",1100,800);
/* create a multi-dimensional array with 'myArray1' and 'myArray2' */
var multiArray=new Array(myArray1,myArray2);

/* access the values and write to the page */
document.write("The element at position [0][0] in multiArray is: "+multiArray[0][0]+"<br />");
document.write("The element at position [0][1] in multiArray is: "+multiArray[0][1]+"<br />");
document.write("The element at position [0][2] in multiArray is: "+multiArray[0][2]+"<br />");
document.write("The element at position [1][0] in multiArray is: "+multiArray[1][0]+"<br />");
document.write("The element at position [1][1] in multiArray is: "+multiArray[1][1]+"<br />");
document.write("The element at position [1][2] in multiArray is: "+multiArray[1][2]+"<br /><br />");

/* nested 'for loops' can achieve the same output more efficiently */
document.write("Nested 'for loops' can achieve the same output: <br />");
for (var i=0;i<multiArray.length;i=i+1)
{
	for (var j=0;j<myArray1.length;j=j+1)
	{
   	document.write(multiArray[i][j]+"<br />")
	}
}
//]]>
</script>

</body>
</html>