1

I know that I can create an associative array like this:

var MyAssocArray = {'sky':'blue', 'grass':'green'};

And I am very fond of using this method.

What I would like to do and am having trouble with is this:

I have strings saved like this:

var MyString = "'sky':'blue', 'grass':'green'";

And I want to be able to do this now:

var MyAssocArray = {MyString};

When I try that - I get this error:

invalid object initializer

What am I doing wrong? How can I achieve this?


I found a different solution using PHP and JavaScript. The associative array string is echoed in the JavaScript code:

var Multidimensional_Arr[Multidimensional_Array_Key_Name] = {<?php echo $String_Pulled_From_Database; ?>}; // i.e. 'sky':'blue', 'grass':'green'

// The same can be done for a one-dimensional array
var My_Single_Dime_Arr = {<?php echo $String_Pulled_From_Database; ?>}; // i.e. 'sky':'blue', 'grass':'green'
0

3 Answers 3

4

Use JSON -- it's serialized JavaScript Object Notation and is pretty close to what you're doing.

You'd need to do

var MyAssocArray = JSON.parse(MyString);

Also, JSON uses double quotes, not single quotes; if you use simple objects, you can probably write code to just replace ' with "" in your strings, but it's tricky if the strings contain double-quotes.

If you are using a browser that doesn't implement JSON.parse(), you can either use the implementation on the JSON website (see links at bottom of this page) or if you're using jQuery, there's jQuery.parseJSON().


Warning: Your solution has a security risk, unless you are sure the data in the database has been sanitized:

 var My_Single_Dime_Arr = {<?php echo $String_Pulled_From_Database; ?>}

This is equivalent to a call to eval(); if your database string has any malicious code it can do bad things. This is one reason why JSON was invented -- it's easy to ensure that its contents are valid (and hence safe) before evaluated.

Your overall architecture, as you have presented it to us, is [data in database] -> server-side PHP -> client-side JavaScript. This is a classic example of serialized data. I realize you may have constraints to keep your system running without interruption, but a strict serialization format would make your system more secure.

Sign up to request clarification or add additional context in comments.

1 Comment

Except, can the JSON object really be recommended without a browser support caveat yet?
0

If you are sure the data is safe, then you could do:

var MyAssocArray = eval('{' + MyString + '}');

2 Comments

Also - I found another method:Since the string is being pulled out of a database - I found that I can populate the javascript associative array using PHP
Yeah.. this is not working for me. I tried it and get the error "-- invalid label"
-1

You can't do that.

To achieve what you want, use the split() function to split your string into comma-separated tokens first. Then each token should further be split by the ':' character.

Then push the two tokens obtained by the last split as the key and the value of your associative array.

1 Comment

that unfortunately won't work if any of the string keys or values has a comma in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.