Ok so! First of all I'm beginning to question my sanity AND Secondly can this be done?
Firstly here's the small range of questions on here that I've found relating to this
javascript array associative AND indexed? (correct question wrong outcome) Double indexing in associative array (not directly related, get the feeling this question is actually asking about 2D Matrix's)
ok so what do I have so far? I have a PHP script that's executing a MySQLi query and spitting out fully formatted JavaScript attached to a HTML page as a < script >. Thus far it's a 3D "array" (yes I'm aware it's an object masquerading as an array). All following Code samples are (supposed to be) JavaScript I'm fairly well acquainted with PHP so we will ignore execution on that end. Here's the rough template of the object I'm ending up with post PHP construction.
var myObj = {
brand1: {
0: brand1, // this exists so that in a "for x in y" loop I can do myObj[x]['0'] to retrieve my brand names as needed
variety1: {
//variations size in litres
0: variety1 // same idea as the "0: brand1" above
1: 0.8
2: 1.0
3: 2.0
},
variety2: {
//same idea as above
},
//continue for all other variations on this brand
},
brand2: {
0: brand2,
variety1: {
//etc.
}, //etc.
}
// continue for other brands and there variations
}
What I understand is happening (bearing in mind I've been using JavaScript for a grand total of 6 days and have not yet run my code and tried to use my data construct)
I can "index" into my "array" via there labels ie myObj.brand1.variation1.0 or myObj['brand2']['variation1']['0'] etc. and so own through the 3D "matrix" I've abstracted. I can also do a loop through the contents of each level of the "matrix" via a "for x in y" loop.
Now my crazy idea. Could you somehow define this nesting of objects with two id's to each object to get what would amount to an indexed AND associative "matrix"? The question arose because I'm already counting as part of my PHP construction script and using that count in some areas already. ie. is it as simple as doing this?
0: brand1: {
0: varition1: {
//everything else is the same as the above template
},
},
or parhaps this is more realistic? (albeit gruesome and inefficient)
0: {
0: {
//my sizes
},
varition1: {
//my sizes
},
},
brand1: {
0: {
//my sizes
},
varition1: {
//my sizes
},
},
or is this truly pushing the capabilities of JavaScript just a little to far?
Just a general question, the assumed negative answer to which has forced me to this JavaScript via PHP process:
Why hasn't anyone built a way to send PHP variables and such to JavaScript (especially those from MySQL queries) one would think it a fairly logical thing to do... ? (sans AJAX naturally, I don't need dynamic data) :|
And more importantly why hasn't a truly associative (and multidimensional) array data type been included in JavaScript?