PDA

View Full Version : Multidimensional / Associative arrays

333qaz333
12-30-2007, 04:56 AM
hi,

I will try to explain what I am trying to do as best as I can, however I think it will be much easier to follow along if you can see a demo.

Okay, so I am trying to work with arrays.

var a = new Array('a1', 'a2');
a['a1'] = 'a1value';
a['a2'] = 'a2value';
var b = new Array('b1', 'b2');
b['b1'] = 'b1value';
b['b2'] = 'b2value';
var ab = new Array(a, b);

When I try to access the array directly, I get the associative name ('a1', 'a2', 'b1', or 'b2') not the value ('a1value', 'a2value', 'b1value', 'b2value').

//should return 'a2value', but instead it returns 'a2'.

At first I thought I was trying to store the value into the variable incorrectly.
However, when I use for(value in array) loops, I get the associative name half the time, but the other half the time I can actually get the value. This means that the value is in the variable, but I don't know how to properly retrieve it.

Here are my for loops:

for(value1 in ab){
for(value2 in ab[value1]){
}
}

Another question, why is my second for loop looping 4 times through an array that only has a length of 2?

This is very confusing, and I don't understand what is happening...
Again, here is the demo (http://333qaz333.no-ip.biz/test3.php) to show you what I am trying to do.

Any help is greatly appreciated!!!

~Brandon

Arty Effem
12-30-2007, 05:26 AM
var a = new Array('a1', 'a2');
a['a1'] = 'a1value';
a['a2'] = 'a2value';
var b = new Array('b1', 'b2');
b['b1'] = 'b1value';
b['b2'] = 'b2value';
var ab = new Array(a, b);

When I try to access the array directly, I get the associative name ('a1', 'a2', 'b1', or 'b2') not the value ('a1value', 'a2value', 'b1value', 'b2value').
Elements created using associative syntax are stored entirely separately, they cannot be indexed numerically and their existence is not reflected in the length property.

Trinithis
12-30-2007, 05:49 AM
I'll use array shorthand. Aka:

//longhand
var x = new Array(1, 2, 3, . . . );
//shorthand
var x = [1, 2, 3, . . . ];

*****************

Okay, so

var a = ["a1", "a2"];

//Same as
var a = [];
a[0] = "a1";
a[1] = "a2";

Given

var a = ["a1", "a2"];
a["a1"] = "a1value";
a["a2"] = "a2value";
var b = ["b1", "b2"];
b["b1"] = "b1value";
b["b2"] = "b2value";
var ab = [a, b];

a looks like:

a
|
-----------------------------
| | | |
| | | |
[0] [1] [a1] [a2]
("a1") ("a2") ("a1value") ("a2value")

b looks similar.

ab looks like:

ab
|
--------
| |
[0] [1]
(a) (b)
| |
... ...

333qaz333
12-30-2007, 09:36 AM
THANK YOU TRINITHIS!!!!!!

Thank you soo much... Your diagram/tree helped me out soo much.

I owe you one...

~Brandon

333qaz333
12-30-2007, 09:41 AM
I have another question now...

I know that this is kinda the opposite of what I was originally asking, but is there anyway for me to get the associative names (pretending that i don't know what they are)? Just curious...

If there's not that's fine, it's not crucial and I can work fine without it, but I was just wondering if there was anyway to do that.

Thank you again, you have saved me from countless hours of frusturation...

rnd me
01-01-2008, 09:19 PM
var aa = [1,2,3];
aa.a="aaa";
aa.b="bbb";

var log="";
for(var i in aa){
log += ( "aa."+ i + "=" + aa[i]+ "\n" );
}

/* output:
aa.0=1
aa.1=2
aa.2=3
aa.a=aaa
aa.b=bbb */

333qaz333
01-01-2008, 09:56 PM

var a = new Array('a', 'b');
a['a'] = 'avalue';
a['b'] = 'bvalue';

/*
values in a are:
a[1] = 'a';
a[2] = 'b';
a['a'] = 'avalue';
a['b'] = 'bvalue';
*/

I have to do this:

var a = [];
a['a'] = 'avalue';
a['b'] = 'bvalue';

/*
values in a are:
a['a'] = 'avalue';
a['b'] = 'bvalue';
*/