最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pass php array of arrays to Javascript - Stack Overflow

programmeradmin3浏览0评论

I have an array of arrays in PHP in this layout for example,

"Key1" => { "id" => 1, "name" => "MyName", "address" => "USA" }
"Key2" => { "id" => 2, "name" => "MyName2", "address" => "Australia" }

The data in the PHP array was taken from SQL Database. Now I want to be able to use this in JavaScript.

I searched the web and people are suggesting to use JSON using this code:

var js_var = JSON.parse("<?php echo json_encode($var); ?>");

I get this error in firefox when using firebug

missing ) after argument list [Break On This Error]
var js_var = JSON.parse("{"Key1":{"id":"1","name":"MyName","address":"USA"...

Error is in right after JSON.parse("{"Key1

In google chrome, firebug does not report any errors

I have an array of arrays in PHP in this layout for example,

"Key1" => { "id" => 1, "name" => "MyName", "address" => "USA" }
"Key2" => { "id" => 2, "name" => "MyName2", "address" => "Australia" }

The data in the PHP array was taken from SQL Database. Now I want to be able to use this in JavaScript.

I searched the web and people are suggesting to use JSON using this code:

var js_var = JSON.parse("<?php echo json_encode($var); ?>");

I get this error in firefox when using firebug

missing ) after argument list [Break On This Error]
var js_var = JSON.parse("{"Key1":{"id":"1","name":"MyName","address":"USA"...

Error is in right after JSON.parse("{"Key1

In google chrome, firebug does not report any errors

Share Improve this question asked Mar 19, 2012 at 20:01 John NgJohn Ng 8993 gold badges15 silver badges29 bronze badges 3
  • 7 use JSON.parse('<?php= json_encode($var); ?>'); (note the single quotes.) – Brad Christie Commented Mar 19, 2012 at 20:03
  • What's the exact value? You've trimmed potentially vital information... – deed02392 Commented Mar 19, 2012 at 20:04
  • possible duplicate of how to pass array string in javascript function from php end as a argument – outis Commented Mar 23, 2012 at 22:19
Add a ment  | 

3 Answers 3

Reset to default 5
var js_var = JSON.parse('<?php echo json_encode($var); ?>');

Or, even better:

var js_var = <?php echo json_encode($var); ?>;

... Since JSON is, by definition, valid JavaScript syntax for object declaration.

Why go through this bizarre json_encode into a string to only JSON.parse on the client side? Useless use of encoding, really.

Try var js_var = <?php echo json_encode($var); ?>;

Your error is being cause by the fact you are using double-quotation marks (") to wrap the JSON string. Due to the fact that the JSON string also contains double-quotations the parser is unable to determine when the string starts and ends correctly.

Change the line to this:

var js_var = JSON.parse('<?php echo json_encode($var); ?>');

That been said JSON or JavaScript Object Notation is already a subset of the JavaScript programming language and therefore can already be parsed by the JavaScript engine so does not necessarily need to be parsed by JSON.parse.

var js_var = <?php echo json_encode($var); ?>;

I would however only remend this option if you are sure about the JSON you are outputting as an incorrect parsing by JSON.parse can be handled where as incorrect JSON injected directly will cause a parser error I believe.

发布评论

评论列表(0)

  1. 暂无评论