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?

h4kl0rd
  • 625
  • 1
  • 10
  • 23
  • Why json encode and then parse in JS? Why not print as-is? Like: `var myVar = = json_encode($myArray) ?>;` – Rudie Feb 14 '14 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. – phpisuber01 Feb 14 '14 at 14:39
  • possible duplicate of [How to access PHP variables in JavaScript or jQuery rather than ](http://stackoverflow.com/questions/1808108/how-to-access-php-variables-in-javascript-or-jquery-rather-than-php-echo-vari) –  Feb 14 '14 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. –  Feb 14 '14 at 20:00

3 Answers3

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

user70585
  • 1,397
  • 1
  • 14
  • 19
4

You need to echo out the json object.

<?php json_encode($myArray) ?>

to

<?php echo json_encode($myArray) ?>
phpisuber01
  • 7,585
  • 3
  • 22
  • 26
  • 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 = ;` – phpisuber01 Feb 14 '14 at 14:37
  • I tried `var myVar = '';` and `var myVar = '= json_encode($notes) ?>';` both return `Uncaught SyntaxError: Unexpected identifier` – h4kl0rd Feb 14 '14 at 16:22
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.

Community
  • 1
  • 1