JSON PHP 数组到 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15488538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON PHP Array to Javascript
提问by ealeon
Is it possible to JSON this php array via json_encode?
Because this php array is called $dataand when i do..
是否可以通过 json_encode JSON 这个 php 数组?因为这个 php 数组被调用$data,当我这样做时..
var myJson = <?php echo json_encode($data) ?>;
console.log(myJson);
in javascript, it give me errors. I was wondering if there is a limitation on what sort of php arrays JSON can/cannot do.
在javascript中,它给了我错误。我想知道 JSON 可以/不能做什么类型的 php 数组是否有限制。
Array
(
[0] => Array
(
[0] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Tanner, C. kickoff 60 yards to the DU5, Butler, L return 14 yards to the DU19 (Johnson, I.).
)
[spot] => SimpleXMLElement Object
(
[0] => GT35
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => K
)
[drive] => 1
)
)
[1] => Array
(
[0] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Renfree, S pass complete to Vernon, C for 6 yards to the DU25 (Thomas, Je.).
)
[spot] => SimpleXMLElement Object
(
[0] => DU19
)
[ball] => SimpleXMLElement Object
(
[0] => DU
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => P
)
[drive] => 2
)
[1] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Renfree, S pass incomplete to Scott, D.
)
[spot] => SimpleXMLElement Object
(
[0] => DU25
)
[ball] => SimpleXMLElement Object
(
[0] => DU
)
[togo] => SimpleXMLElement Object
(
[0] => 4
)
[type] => SimpleXMLElement Object
(
[0] => P
)
[drive] => 2
)
[2] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Renfree, S pass complete to Scott, D for 6 yards to the DU31, 1ST DOWN DU (Thomas, Je.).
)
[spot] => SimpleXMLElement Object
(
[0] => DU25
)
[ball] => SimpleXMLElement Object
(
[0] => DU
)
[togo] => SimpleXMLElement Object
(
[0] => 4
)
[type] => SimpleXMLElement Object
(
[0] => P
)
[drive] => 2
)
[3] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Renfree, S pass complete to Scott, D for no gain to the DU31 (Sweeting, R.).
)
[spot] => SimpleXMLElement Object
(
[0] => DU31
)
[ball] => SimpleXMLElement Object
(
[0] => DU
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => P
)
[drive] => 2
)
[4] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Renfree, S pass incomplete to Vernon, C.
)
[spot] => SimpleXMLElement Object
(
[0] => DU31
)
[ball] => SimpleXMLElement Object
(
[0] => DU
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => P
)
[drive] => 2
)
[5] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Renfree, S sacked for loss of 4 yards to the DU27 (Gotsis, A.).
)
[spot] => SimpleXMLElement Object
(
[0] => DU31
)
[ball] => SimpleXMLElement Object
(
[0] => DU
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => P
)
[drive] => 2
)
[6] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Monday, W punt 53 yards to the GT20, Golden, J. return 8 yards to the GT28 (Reeves, D).
)
[spot] => SimpleXMLElement Object
(
[0] => DU27
)
[ball] => SimpleXMLElement Object
(
[0] => DU
)
[togo] => SimpleXMLElement Object
(
[0] => 14
)
[type] => SimpleXMLElement Object
(
[0] => U
)
[drive] => 2
)
)
[2] => Array
(
[0] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Smith, O. rush for 8 yards to the GT36 (Brown, Ky).
)
[spot] => SimpleXMLElement Object
(
[0] => GT28
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[1] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Sims, D. rush for 6 yards to the GT42, 1ST DOWN GT (Sink, N).
)
[spot] => SimpleXMLElement Object
(
[0] => GT36
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 2
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[2] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Washington, T. pass incomplete to Smith, O..
)
[spot] => SimpleXMLElement Object
(
[0] => GT42
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => P
)
[drive] => 3
)
[3] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Washington, T. rush for 2 yards to the GT44 (Anunike, K).
)
[spot] => SimpleXMLElement Object
(
[0] => GT42
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[4] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Greene, J. rush for 6 yards to the 50 yardline (Canty, W).
)
[spot] => SimpleXMLElement Object
(
[0] => GT44
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 8
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[5] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Smith, O. rush for 16 yards to the DU34, 1ST DOWN GT (Canty, W;France, C).
)
[spot] => SimpleXMLElement Object
(
[0] => GT50
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 2
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[6] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Smith, O. rush for 5 yards to the DU29 (France, C).
)
[spot] => SimpleXMLElement Object
(
[0] => DU34
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[7] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Laskey, Z. rush for 3 yards to the DU26 (France, C).
)
[spot] => SimpleXMLElement Object
(
[0] => DU29
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 5
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[8] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Washington, T. rush for 3 yards to the DU23, 1ST DOWN GT (France, C).
)
[spot] => SimpleXMLElement Object
(
[0] => DU26
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 2
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[9] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Washington, T. pass complete to Smith, O. for 21 yards to the DU2, 1ST DOWN GT (Butler, L).
)
[spot] => SimpleXMLElement Object
(
[0] => DU23
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 10
)
[type] => SimpleXMLElement Object
(
[0] => P
)
[drive] => 3
)
[10] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Washington, T. rush for 1 yard to the DU1 (Brown, Ky;Foxx, J).
)
[spot] => SimpleXMLElement Object
(
[0] => DU02
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 0
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[11] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Washington, T. rush for no gain to the DU1 (Cockrell, R).
)
[spot] => SimpleXMLElement Object
(
[0] => DU01
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 0
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[12] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Washington, T. rush for 1 yard to the DU0, TOUCHDOWN, clock 06:51.
)
[spot] => SimpleXMLElement Object
(
[0] => DU01
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 0
)
[type] => SimpleXMLElement Object
(
[0] => R
)
[drive] => 3
)
[13] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Tanner, C. kick attempt good.
)
[spot] => SimpleXMLElement Object
(
[0] => DU03
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 0
)
[type] => SimpleXMLElement Object
(
[0] => X
)
[drive] => 3
)
[14] => Array
(
[text] => SimpleXMLElement Object
(
[0] => Tanner, C. kickoff 54 yards to the DU11, Butler, L return 23 yards to the DU34 (Harrell, A.).
)
[spot] => SimpleXMLElement Object
(
[0] => GT35
)
[ball] => SimpleXMLElement Object
(
[0] => GT
)
[togo] => SimpleXMLElement Object
(
[0] => 0
)
[type] => SimpleXMLElement Object
(
[0] => K
)
[drive] => 3
)
)
)
回答by
First you need a PHP file on a Apache server somewhere with PHP installed. Make a file like this:
首先,您需要一个安装了 PHP 的 Apache 服务器上的 PHP 文件。制作一个这样的文件:
localhost:8888/myfile.php
localhost:8888/myfile.php
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}
?>
Then your JavaScript (in this example I use jQuery):
然后你的 JavaScript(在这个例子中我使用jQuery):
$.getJSON('http://localhost:8888/myfile.php', function(data) {
console.log(data);
});
This should be a start to get PHP arrays in to your JavaScript.
这应该是将 PHP 数组引入 JavaScript 的开始。
回答by Nick Blanchard-Wright
As @Allendar said you can't embed PHP inside a JS file. You could, however, add a function in your JS file to load the JSON data, and then embed that data in a script tag in your PHP file.
正如@Allendar 所说,您不能将 PHP 嵌入到 JS 文件中。但是,您可以在 JS 文件中添加一个函数来加载 JSON 数据,然后将该数据嵌入 PHP 文件的脚本标记中。
example.js:
例子.js:
var loadJsonFromPHP = function(json) {
console.log(json);
}
example.php:
例子.php:
<?php
$data = array("some", "test", "data");
?>
<html>
<head>
<script src="example.js"></script>
<script>
loadJsonFromPHP(<?php echo json_encode($data) ?>);
</script>
</head>
<body></body>
</html>
Edit: this is assuming you only need to get the data into JS once at page load, in which case you can skip making AJAX requests.
编辑:这是假设您只需要在页面加载时将数据导入 JS 一次,在这种情况下您可以跳过 AJAX 请求。
回答by Kashinath Patil
Passing PHP JSON to Javascript and reading
将 PHP JSON 传递给 Javascript 并读取
var stuff = <?php print json_encode($datajson); ?>;
var arr = new Array();
arr= JSON.parse(stuff);
document.write( arr[0].cust_code );
var stuff = <?php print json_encode($datajson); ?>;
var arr = new Array();
arr= JSON.parse(stuff);
document.write( arr[0].cust_code );
回答by Nick Andriopoulos
JSON can handle any type of array (albeit it will cast associative arrays as objects). The problem you are probably facing is that you are trying to output with PHP when the data is available only on Javascript.
JSON 可以处理任何类型的数组(尽管它将关联数组转换为对象)。您可能面临的问题是,当数据仅在 Javascript 上可用时,您正尝试使用 PHP 输出。
To clarify: once the page has loaded, PHP cannot do anything. Only javascript can process things on client side, PHP works only on the server and has no knowledge of the state of the client.
澄清一下:一旦页面加载完毕,PHP 将无法执行任何操作。只有 javascript 可以在客户端处理事情,PHP 只能在服务器上工作,并且不知道客户端的状态。
回答by Aaron Hathaway
I've never tried to do something like this but I think that you're having issue because json_encode returns a json encoded string. You then need to decode this string on the javascript side of things. Try something like the following:
我从来没有尝试过做这样的事情,但我认为你有问题,因为 json_encode 返回一个 json 编码的字符串。然后,您需要在 javascript 方面解码此字符串。尝试类似以下内容:
var myJson = JSON.parse(<?php echo json_encode($data) ?>);
console.log(myJson);

