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

  • Home
  • SEARCH
  • 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 8393749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:52:56+00:00 2026-06-09T19:52:56+00:00

i have encoded my required data in the json object ,but i want to

  • 0

i have encoded my required data in the json object ,but i want to decode the json object into a javscript array, my json encoded object is :

{"product_id":"62","product_quantity":"65"}

however i want to use this json in my java script and want it to be available to a java script array

so if i do :

var arr = new Array()
arr = <?php json_decode('$json_object',TRUE); ?>;

however when i check my page source i get null i.e arr =
how can i assign my json object converted to array to java script array ?
further how to access the json objects from java script array ?

  • 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-09T19:52:58+00:00Added an answer on June 9, 2026 at 7:52 pm

    json_decode returns a PHP data structure. If you want to serialise that to a JavaScript data structure you have to pass it through json_encode (and then actually echo the string that it returns).

    Note that json_encode outputs a JavaScript data structure that is safe for injecting into a <script> element in an HTML document. Not all JSON is safe to do that with (PHP adds additional escape sequences, and will transform plain strings, numbers, null values, etc (which aren’t legal JSON on their own).

    Note that there is also no point in creating a new array and assigning it to arr if you are going to immediately assign something else to arr.

    Also note that '$json_object' will give you a string starting with the $ character and then the name of the variable. Single quoted string in PHP are not interpolated.

    var arr;
    arr = <?php echo json_encode(json_decode($json_object,TRUE)); ?>;
    

    Also note that this JSON:

    {"product_id":"62","product_quantity":"65"}
    

    Will transform in to a PHP associative array or a JavaScript object (which is not an array).

    So given this PHP:

    <?php
        $json_object = '{"product_id":"62","product_quantity":"65"}';
    ?>
    <script>
        var arr;
        arr = <?php echo json_encode(json_decode($json_object,TRUE)); ?>;
        alert(arr.product_id);
    </script>
    

    You get this output:

    <script>
        var arr;
        arr = {"product_id":"62","product_quantity":"65"};
        alert(arr.product_id);
    </script>
    

    Which alerts 62 when run.

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

Sidebar

Related Questions

I have PHP generating JSON data but I am having a problem making a
I am inserting smileys into strings. I have encoded the smileys in a certain
I currently have an encoded footer file for a wordpress file I want to
I have UTF-8 encoded NSData from windows server and I want to convert it
I have an UTF-8 encoded txt file and I want to import it to
I have string that displays UTF-8 encoded characters, and I want to convert it
I am learning json now and have run into an issue: I am using
I'm creating JSON encoded data from PHP arrays that can be two or three
Well I have a problem reading the JSON data in jQuery, which is outputted
I create a huge JSON-Object and save it in my database. But when I

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.