PDA

View Full Version : how do i populate this multudimension array?



ricmetal
06-18-2011, 05:57 PM
hi
new to multi dimension arrays and not very bright.

who do i populate a multidimension array from a single string?
the string is formatted like this:


var resultVar = "id1,photo1,thumb1,description1,id2,photo2,thumb2,description2,id3,photo3,thumb3,description3";

thanks

vwphillips
06-19-2011, 08:34 AM
<!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">

<head>
<title></title>
</head>

<body>
<script type="text/javascript">
/*<![CDATA[*/
var resultVar = "id1,photo1,thumb1,description1,id2,photo2,thumb2,description2,id3,photo3,thumb3,description3";

var split=resultVar.split(',');
var ary=[];
var length=4;

for (var nu,z0=0;z0<split.length;z0++){
nu=Math.floor(z0/length);
if (!ary[nu]){
ary[nu]=[];
}
ary[nu].push(split[z0]);
}

alert(ary.join('\n'));
/*]]>*/
</script>
</body>

</html>

ricmetal
06-19-2011, 08:52 AM
okay, i can see the alert with the values divided, i assume i will be able to understand how to use your code. thanks.

but for sake of me learning how to do this in simpler terms which i understand, how would i finish the following code?



<script type="text/javascript">

var resultRaw = "3,1a_2a_3a_4a-1b_2b_3b_4b-1c_2c_3c_4c";
var charAtPos = resultRaw.indexOf(',');
var items = resultRaw.substring(0, charAtPos);
var resultVar = resultRaw.substr(charAtPos + 1);

var simpleArray = resultVar.split("-");
// simpleArray[0] = 1a_2a_3a_4a;
// simpleArray[1] = 1b_2b_3b_4b;
// simpleArray[2] = 1c_2c_3c_4c;
// simpleArray[3] = 1d_2d_3d_4d;


var rowArray = simpleArray[0].split("_");
// rowArray[0] = 1a;
// rowArray[1] = 2a;
// rowArray[2] = 3a;
// rowArray[3] = 4a;

</script>


i'd like to end up with something like



dataArray[0][0] = 1a;
dataArray[0][1] = 2a;
dataArray[0][2] = 3a;
dataArray[0][3] = 4a;
dataArray[1][0] = 1b;
dataArray[1][1] = 2b;
//etc..


i like the fact that the split automatically creates an array, i just havent found out how to implement it yet in a 2d array.


var newArray = somestring.split(",");


EDIT

nevermind, figured it out.