1

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?

Community
  • 1
  • 1
azariah
  • 439
  • 4
  • 15
  • Please try and omit the clever innuendo's in the question. Since you have a long question it becomes very cumbersome to read so try and avoid the jokes. – MarsOne Aug 28 '13 at 06:34
  • Can't you just `json_encode($data)` and work with that? – elclanrs Aug 28 '13 at 06:35
  • @MarsOne sorry.... :D can't help it. I'm a writer at heart so... yeah. :) I'll try to keep things shorter in future. – azariah Aug 28 '13 at 06:42
  • @elclanrs you're referring to the copying of PHP variables to JavaScript yeah? I wasn't aware of this I'll look it up. :) – azariah Aug 28 '13 at 06:43
  • @azariah, No problem. We do like a bit of a chuckle sometimes. No hard feelings i hope? – MarsOne Aug 28 '13 at 06:47
  • @MarsOne :P of course not. :D I'm still learning to be professional in my language constructs so I appreciate constructive comments. I'm intending on open sourcing my project once I get something somewhat operational so... I'll need all the communication skills help I can get. :) – azariah Aug 28 '13 at 06:50
  • @elclanrs json_encode($data) is pretty much what I'm doing manually wish id found that earlier. Um but I'll bring it back to my original question is there a way to "double index" these object matrixes? IE add the functionality expected of associative arrays in other language eg PHP. – azariah Sep 03 '13 at 02:15

0 Answers0