mrbean Posted August 13, 2012 Share Posted August 13, 2012 Why doesn't this work? var candy = new Array(new Array()); for (var i = 0; i < 10; i++) { candy[i][i] = "Sweet!"; } The error that I get from chrome is: Uncaught TypeError: Cannot set property '0' of undefined I want to make an multidimensional array in an loop without knowing how long it can be. Quote Link to comment https://forums.phpfreaks.com/topic/266997-multidimensional-array/ Share on other sites More sharing options...
nogray Posted August 13, 2012 Share Posted August 13, 2012 Because you only declare the index candy[ 0 ] as array, candy[ 1 ] is not defined. You can simply declare the arrays in your loop var candy = []; for (var i=0; i<10; i++){ candy[i] = []; candy[i][i] = 'Sweet!'; } Quote Link to comment https://forums.phpfreaks.com/topic/266997-multidimensional-array/#findComment-1369129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.