Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7593375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:09:13+00:00 2026-05-30T21:09:13+00:00

I know that I can create an associative array like this: var MyAssocArray =

  • 0

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'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:09:14+00:00Added an answer on May 30, 2026 at 9:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that horizontal partitioning...you can create many tables. How can you do this
I would like to know how to create a php function that can be
I'm using python 3.1.1. I know that I can create byte objects using the
I know that we can't overload operator with other meaning, we can't create new
I wish to know is there any way that I can create the threads
Does anyone know of any software that can be used to create Agile User
Does anyone know how I can Dynamically create an installation package that installs files
I know that I can do something like $int = (int)99; //(int) has a
Is it possible to return groups as an associative array? I'd like to know
I want to create an associative array: var aa = {} // Equivalent to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.