2

I have a php nested array stored in a variable $myArray, below is how the array looks like (its not a complete output) after var dumping it to the browser.

<?php var_dump($myArray); ?>

The output:

array (size=4)
  'id' => string '162' (length=3)
  'content' => string 'Test content' (length=12)
  'children' => 
    array (size=16)
      0 => 
        array (size=4)
          'id' => string '29208' (length=5)
          'content' => string 'Test content 1' (length=14)
          'children' => 
            array (size=3)
              ...
      1 => 
        array (size=4)
          'id' => string '29215' (length=5)
          'content' => string 'Test content 2' (length=14)
          'children' => 
            array (size=1)
              ...
      2 => 
        array (size=3)
          'id' => string '29220' (length=5)
          'content' => string 'Test Content 3' (length=14)

Reading the variable array from JavaScript as below:

<script type="text/javascript">
var myVar = JSON.parse('<?php json_encode($myArray) ?>');
</script> 

Returns the following error in the console

Uncaught SyntaxError: Unexpected end of input

While debugging the code, I did the following:

Created a new variable and stored some JSON data in it and then JSON parsed it to another variable and then finally consoled the output and it worked fine.

<script type="text/javascript">
var x = '{"id":123,"content":"This is a test content"}';
var myVar = JSON.parse(x);
console.log(myVar); 
</script> 

The output was an object with those values in the console:

Object
    content: "This is a test content"
    id: 123

What am I doing wrong?

4
  • Why json encode and then parse in JS? Why not print as-is? Like: var myVar = <?= json_encode($myArray) ?>; Commented Feb 14, 2014 at 14:36
  • Rudie is correct, you do not need to parse the JSON as a string when outputting it directly to the browser in this way. Commented Feb 14, 2014 at 14:39
  • possible duplicate of How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> Commented Feb 14, 2014 at 19:59
  • This question is answered in that question where you should've looked. He clearly shows how to use an object instead of a string and states that quotes will break the script. Commented Feb 14, 2014 at 20:00

3 Answers 3

6

var myVar = <?php echo json_encode($myArray) ?>; should do it. No ' characters are needed because the JSON object can be read as written, and no parse is necessary because it's outputting directly onto the page instead of giving it a string

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

Comments

4

You need to echo out the json object.

<?php json_encode($myArray) ?>

to

<?php echo json_encode($myArray) ?>

2 Comments

No, JSON.parse parses a string into a javascript object. If you take out the single quotes you're converting a Javascript Object to a Javascript Object. Essentially in this form, you don't even need the JSON.parse, you could set the variable directly from the json_encode. i.e. var obj = <?php echo json_encode($myArray); ?>;
I tried var myVar = '<?php echo json_encode($notes); ?>'; and var myVar = '<?= json_encode($notes) ?>'; both return Uncaught SyntaxError: Unexpected identifier
3

Here's a little shorthand trick for you explained here

You can simply do <?=$var?>. It's basically shorthand for echo and only works if the shorthand tag <? is enabled.

So the answer to your question (if shorthand open tags are enabled) you can use this

var myVar = <?=json_encode($myArray)?>; Which is equivalent to what @Dar gave you above but less ugly.

Comments

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.