The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

On an HTML page I have a form with two radio buttons for each option in the above array. (Yes/No) At the top of the page, I have a selection of buttons representing ['typeX'] , each with an "onClick" property.

What I want to happen is that when you click on a button, the options for that type will appear in the form, using the settings from the PHP array.

How can I do this? I presume I'm going to need to convert the PHP array into a javascript one, can this be done?

Those aren't really javascript arrays - those are objects. I've had the argument with someone before that javascript objects are associative arrays, but don't be fooled, they aren't associative arrays like you know them in PHP.

I've always used this type of method and I guess I have always considered them to be associative arrays in JS, without ever really thinking about it too much, so by all means continue, I am most interested in your feedback.

Okay, here's the lowdown as best as I understand, and can describe it.

First of all, a couple things about JS.
1) Everything is an object. Even primitives have methods and properties (such as String.indexOf).
2) JSObjects support two methods of reference: dot-syntax, and bracket-syntax. Object.property is the same as Object['property'].

By reasoning of these two rules, and observing the following code

Code:

var arr = new Array();
arr['index1'] = "value1";

We can reason that index1 is actually a property that we're attaching to this instance of the Array object, and not a named (or keyed) index. This is the same as doing:

Code:

arr.index1 = "value";

So far, it still looks like an associative array -- mostly. I should take a moment to expound on the syntax differences. Dot-syntax allows only alphanumeric characters in for property names that must start with an alpha character, or the underscore, just like variables. This is partly because it's bad practice, and partly because it can cause problems with Array objects, which we'll see later. Since using bracket syntax allows you to assert the property names as strings, you can both create and reference what would normally be invalid property tokens. It is this very reason why we must use the elements collection to reference form elements whose name contains a space, or is itself named like an array (form.elements['chexbox[]']), or some other non-alphanumeric character.

Now, let's look at a regular array in JS and, being an object, some of the properties it has

We can see that each numerical index is included in it's length property, and that the numerical indexes are behaving as expected. Now, let's toss a monkey wrench in here.

Code:

arr['10'] = 10;

What did I just create? A property? Or an index? According to my rules, the above should be a property named 10. Let's see

Code:

alert( arr ); // 1,2,3,4,,,,,,,10

Ahhh, so it's not a property after all! According to the alert, I just stored the number 10 into the 10th index of the array. Herein lies the exception to the rule, and why I feel javascript doesn't provide a true associative data structure to the user.

If arr was an instance of Object, and not Array, then arr['10'] would be a property, albeit one only accessed by the bracket notation per javascript tokenization rules. But, since arr is not an Object instance, the language interprets my ['10'] as [10], therefore disallowing me to create an array with named keys that are solely numeric in composition while simultaneously disallowing me to create a property named 10 as well.

Furthermore, properties added to an Array object aren't part of it's vector - meaning the length property doesn't count for their existence, nor will an iterative loop catch them. A for...in loop does, however, it will also grab any other user-defined properties on the page as well - something I couldn't handle, since I use object extensions regularly.

So, both Object and Array give an associative array implementation to the user, as I see it, only halfway. It is only because Javascript's script engine implements everything as objects, that we are allowed to add named properties. By that reasoning, you might as well use any datatype to build your associative array on, such as Function, String, or Boolean -- which I think we all recognize is just a no-no.

True...
However unless I'm missing something, the first 2 alerts should alert the same result -- 'noNum' -- because they are not numerical idicies. Is it that -- "10" does not equal '10' -- which would explain why the first alert reports 'undefined' ?

What I want to happen is that when you click on a button, the options for that type will appear in the form, using the settings from the PHP array.

All he is asking to do here is to show/hide a <div> full of check-boxes depending on the state of another check-box, or a button on the form. It is not necessary to convert the PHP array into a JavaScript array for this.

I PM'ed him a while ago to make sure he got the help he needed, and he was satisfied - it was only because of that, that I pursued the off topic discussion. I couldn't be a good Mentor without doing so