3 Replies - 382 Views - Last Post: 18 July 2012 - 06:31 AM

#1 vmicchia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-January 12

Issues with multi-dimensional array

Posted 17 July 2012 - 01:43 PM

I apologize if this has been answered but I've been trying for a couple hours and didn't find anything.

My problem is that I am trying to give values to a multi-dimensional array but every thing after the first value is being returned as undefined. I'm doing this with the intention of turning it into a JSON string and saving it to local storage.

Here's the code:
if (typeof order == "undefined" || !(order instanceof Array)) {
                    var order = [];
                }
                var arlen = order.length;
                order[arlen] = arlen;
                order[arlen][0] = po;
                order[arlen][1] = $('#orderform' + t + ' input[name*="eID"]').val();
                order[arlen][2] = '<?php echo $_SESSION['id']; ?>';
                order[arlen][3] = note;
                for(q=0;q<len;q++){
                    if($('#orderform' + t + ' input[class*="check' + q + '"]').is(':checked')){
                        var amt = $('#orderform' + t + ' input[class*="amt' + q + '"]').val();
                        var eid = $('#orderform' + t + ' input[class*="check' + q + '"]').val();
                        order[arlen][4] = [eid, amt];
                    }
                }
                var adhoc = $('#adhoctable > tbody > tr').length;
                if(adhoc > 0){
                 for(z=1; z<=adhoc; z++){
                    var aheid = $('#ahtr'+z.toString()+' > td.aheid').html();
                    var ahamt = $('#ahtr'+z.toString()+' > td.ahamt').html();
                    order[arlen][5] = [aheid, ahamt];
                 }
                }


Thanks for any help you guys can give me.

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with multi-dimensional array

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Issues with multi-dimensional array

Posted 18 July 2012 - 01:34 AM

Hello there!

If you take a look at the snippet of code below and pay attention to the line included with some comments.

<script>
	var arrayOfDoom = new Array();
	
	arrayOfDoom[0] = "Oranges";
	
	//This is the missing part of your code!
	//It's important that you set the next dimension as an array also!
	arrayOfDoom[1] = new Array();
	
	arrayOfDoom[1][0] = "Opera";
	arrayOfDoom[1][1] = "Internet Explorer";
	
	document.write(arrayOfDoom[0]);
	document.write(arrayOfDoom[1][0]);
	document.write(arrayOfDoom[1][1]);
</script>


Basically, unlike some languages, you cannot just declare a second dimension an array automatically by trying to assign values to it like it was an array, you need to actually declare it as an array.

I hope this helps you out!
Was This Post Helpful? 0
  • +
  • -

#3 vmicchia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-January 12

Re: Issues with multi-dimensional array

Posted 18 July 2012 - 06:30 AM

That makes a lot of sense. Thank you very much. I didn't know each new level had to be declared as a new array.
Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Issues with multi-dimensional array

Posted 18 July 2012 - 06:31 AM

Glad I could be of help to you, happy programming!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1