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 9273999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:14:53+00:00 2026-06-18T16:14:53+00:00

I have a code that encodes a javascript object into something like this: <script>

  • 0

I have a code that encodes a javascript object into something like this:

<script>
    var php_string='array("one"=>"value_one","two"=>"value_two")';
</script>

Now I would like to know how can I convert this into that:

{
    "one":"value_one",
    "two":"value_two"
}

The content of php_string is sent to PHP on a $_POST variable. Is there anyway to do that or should I use JSON instead?

EDIT

As asked, here is the code that made my php_string:

function encodeFormOptions(inputsWrapper){
    var the_serialize=$(inputsWrapper).find(":input").serializeArray();
    var text="array(";
    $.each( the_serialize, function( key, value ) {
        text+='"'+value["name"]+'"=>'+'"'+value["value"]+'",';
    });
    if (text.charAt(text.length-1)==",")
        text=text.substring(0,text.length-1);
    text+=");";
    return text;
}

EDIT 2

I forgot to say that I need this conversion in pure Javascript. In other words, I need to parse the array string into a javascript Object because when a click on a certain button on my page occurs, I need to read the values that are currently inside php_string.

EDIT 3 (The full reason of the php_string)

I tried to abstract the problem but, now I see that I shouldn’t have done it.

I have a page form that have some input’s and multiple buttons named “options”

When I click on any of these buttons, a popup appears with dynamic fields (derived from the option selected on a select field) on it and, when I fill all the input’s, I create the php_string and save it on an hidden field.

Now I want that when I click again on one of the “options” buttons, I could read what I saved on the hidden field, in order to put the default values that the popup input’s should show if the selected option of the select field doesn’t change.

The hidden field is then parsed on PHP.

  • 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-06-18T16:14:55+00:00Added an answer on June 18, 2026 at 4:14 pm

    Edit:

    It seems this may be closer what you are looking for:

    <script>
        var php_string = 'array("one"=>"value_one","two"=>"value_two")';
        var php_string = JSON.parse(php_string.replace(/array\(/g, '{').replace(/\)/g, '}').replace(/=>/g, ':'));
    </script>
    

    It’s certainly not the most elegant way to handle it though, so consider refactoring what is producing that output. For example, you could send json from the client and use json_decode in the server side.

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

Sidebar

Related Questions

I have code that looks like this: obj.foo(); // obj might, or might not
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
So I have a php script that does this: $rows = array(); while($r =
I have code that looks more or less like the code below but it
I have a C# ASP.NET app that creates a JavaScript array of values for
My searching skills seems to have failed me. I have this php object that
I have a PHP script that parses an array using the json_encode() method but
I have JavaScript code that retrieves numerical vectors from a web-service. The original data
I have following Javascript code that creates a button element that has a click

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.