By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,400 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,400 IT Pros & Developers. It's quick & easy.

two dimensional array

100+
P: 178
Hi,

I have a javascript function that reads in an array from php. Each record is split by a $ and each field is then split by a ^. I was wondering if anyone could help me get this data into a 2d array? Heres what I have so far.

Expand|Select|Wrap|Line Numbers
  1. function drawExisting() {
  2.     if (request.readyState ==4) {
  3.         var returned = request.responseText;
  4.         var recordSplit = returned.split("$");
  5.         var record = recordSplit[0];
  6.         alert(record);
  7.         var fieldSplit = record.split("^");
  8.         var object_name = fieldSplit[0];
  9.         var xpos = fieldSplit[1];
  10.         var ypos = fieldSplit[2];
  11.         var height = fieldSplit[3];
  12.         var width = fieldSplit[4];
  13.         // alert(object_name + " " + xpos + " " + ypos + " " + height + " " + width);
  14.  
  15.         }
  16. }    
This gives me the results for the first record
Aug 21 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
Loop over recordSplit (an array) and set each fieldSplit (also an array) to an array variable.
Aug 21 '08 #2

100+
P: 178
The problem that is throwing me is that you have to declare the size of the array. Can I declare an array without the size?
Aug 21 '08 #3

acoder
Expert Mod 15k+
P: 16,027
You don't need to declare the size of the array. You can declare it like this:
Expand|Select|Wrap|Line Numbers
  1. var arr = [];
and that's it.
Aug 21 '08 #4

100+
P: 178
Am I on the right track here?

Expand|Select|Wrap|Line Numbers
  1. function drawExisting() {
  2.     if (request.readyState ==4) {
  3.         var record = [];
  4.         var returned = request.responseText;
  5.         var recordSplit = returned.split("$");
  6.         for (i=0; i < record.length; i++) {
  7.             record = recordSplit[i];
  8.             var fieldSplit = record.split("^");
  9.             var object_name = fieldSplit[0];
  10.             var xpos = fieldSplit[1];
  11.             var ypos = fieldSplit[2];
  12.             var height = fieldSplit[3];
  13.             var width = fieldSplit[4];
  14.             alert(object_name + " " + xpos + " " + ypos + " " + height + " " + width);
  15.         }
  16.  
  17.         }
  18. }    
Aug 21 '08 #5

acoder
Expert Mod 15k+
P: 16,027
Sort of. The loop should be on recordSplit, not record. Then use the push() method to add the fieldSplit array to record.
Aug 21 '08 #6

100+
P: 178
I have cracked it. Thanks for your help all the same. Just took a while for the concept to snk in to my thick head. Heres my code.

Expand|Select|Wrap|Line Numbers
  1. function drawExisting() {
  2.     if (request.readyState ==4) {
  3.         cv = document.getElementById("canvas");
  4.         var returned = request.responseText;
  5.         var splitResult = returned.split("$");
  6.         for (var i=0;i<splitResult.length-1;i++) {
  7.             var record = splitResult[i];
  8.             // alert(record);
  9.             var splitRecord = record.split("^");
  10.                 var newObject = document.createElement('div');
  11.                 newObject.id=splitRecord[0];
  12.                 newObject.innerHTML=splitRecord[0];
  13.                 newObject.style.height=splitRecord[3]/10;
  14.                 newObject.style.width=splitRecord[4]/10;
  15.                 newObject.style.top=splitRecord[2];
  16.                 newObject.style.left=splitRecord[1];
  17.                 newObject.onmousedown=function(){grab(this);}
  18.                 cv.appendChild(newObject);
  19.  
  20.         }
  21.         }
  22. }    
Aug 22 '08 #7

acoder
Expert Mod 15k+
P: 16,027
So you didn't need to use a two dimensional array in the end?
Aug 22 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.