javascript 如何从php数组制作java脚本数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8413587/
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
How to make a java script array from php array?
提问by user1029108
Iam using PHP,Smarty configuration.
我正在使用 PHP,Smarty 配置。
Iam getting an array in PHP and sent it to tpl.I would like to make a javascript array in tpl from that php array.
我在 PHP 中获取一个数组并将其发送到 tpl。我想从该 php 数组在 tpl 中创建一个 javascript 数组。
PHP ARRAY
PHP数组
Array
(
[0] => Array
(
[dt] => 2011-12-02
[number] => 3
)
[1] => Array
(
[dt] => 2011-12-05
[number] => 3
)
[2] => Array
(
[dt] => 2011-12-07
[number] => 2
)
)
and I want to get it as a java script array in tpl
我想将它作为 tpl 中的 java 脚本数组
s1 = [[[2011-12-02, 3],[2011-12-05,3],[2011-12-07,2]]];
回答by JohnP
You'd have to loop through it to generate that array
您必须遍历它以生成该数组
<script>
var arr = [];
<?php foreach ($arr as $item) : ?>
arr.push(['<?php echo $item['dt']?>', <?php echo $item['number']?>]);
<?php endforeach; ?>
</script>
This will give you an array of arrays.
这将为您提供一组数组。
However, I would do it in another way. I'd just encode it with json and use it as an object
但是,我会用另一种方式来做。我只是用 json 对其进行编码并将其用作对象
<script>
var data = <?php echo json_encode($arr)?>;
for (var x in data) {
//data[x].dt;
//data[x].number;
}
</script>
回答by Nenad
You can use the php function json_encode to encode the array to json and then use it as an array like object in javascript
您可以使用 php 函数 json_encode 将数组编码为 json,然后将其用作 javascript 中类似对象的数组
回答by Matt Esch
I really dislike the idea of inline PHP all over the place. We don't inline JavaScript or CSS these days because it's bad practice i.e. difficult to maintain. We appreciate separation of concerns. If you need to pass data from your application (PHP or otherwise) to JavaScript variables on the client side then there are two good mechanisms you can use. Either get the result as JSON via AJAX, or simply render the variables on the client side in a single accessible location.
我真的不喜欢到处都是内联 PHP 的想法。我们现在不内联 JavaScript 或 CSS,因为这是不好的做法,即难以维护。我们欣赏关注点的分离。如果您需要将数据从您的应用程序(PHP 或其他)传递到客户端的 JavaScript 变量,那么您可以使用两种很好的机制。要么通过 AJAX 以 JSON 形式获取结果,要么简单地在客户端的单个可访问位置呈现变量。
Your templating engine should really be able to register the data on the page for you. What you are essentially doing is rendering a script, so your script include mechanism should take an optional argument for registering php variables. If you are not using a templating engine and only need to inject the variables, I guess it would be acceptable to do something along the lines of.
您的模板引擎应该真的能够为您注册页面上的数据。您本质上所做的是呈现脚本,因此您的脚本包含机制应该采用可选参数来注册 php 变量。如果您不使用模板引擎而只需要注入变量,我想按照以下方式做一些事情是可以接受的。
<script type="text/javascript">
var phpdata = <?php echo json_encode($phpdata)?>
</script>
Note that this is declaring a global. We are encouraged to avoid the usage of globals at all costs but I think it's a valid use case when sensibly chosen, such as if you register your own object in global scope as a sensible namespace, then refer to namespace.phpdata. Alternatively you simply wrap your JavaScript in an immediate function call and declare the variable in local scope.
请注意,这是声明一个全局变量。我们鼓励不惜一切代价避免使用全局变量,但我认为这是明智选择的有效用例,例如,如果您在全局范围内将自己的对象注册为合理的命名空间,然后参考 namespace.phpdata。或者,您只需将 JavaScript 包装在立即函数调用中并在本地范围内声明变量。
function () {
var phpdata = <?php echo json_encode($phpdata)?>
...
}();
But like I said, in my opinion I think it's better to get your templating engine to handle it, so you pass the PHP data you need for each javascript include, and a single php data object is generated from this data and included in your page.
但是就像我说的,在我看来,我认为最好让您的模板引擎来处理它,因此您传递每个 javascript 包含所需的 PHP 数据,然后从这些数据生成单个 php 数据对象并将其包含在您的页面中.
回答by KingCrunch
First of all: The arrays are not equivalent.
首先:数组不等价。
$array = array_map('array_values', $array);
this one is equivalent to the one you want to see as JS-array
这个相当于你想看的 JS-array
$js = json_encode($array);
If you want to keep the keys of the associative (inner) arrays, just omit the first expression.
如果要保留关联(内部)数组的键,只需省略第一个表达式。
回答by socha23
If you want to do it in the smarty template, loop over your PHP array and output the javascript array elements, adding commas after every element except the last one.
如果您想在 smarty 模板中执行此操作,请遍历您的 PHP 数组并输出 javascript 数组元素,在除最后一个元素之外的每个元素后添加逗号。
s1 = [
{foreach from=$myArray item=item name=loop}
[{$item['dt']}, {$item['number']}] {if $smarty.foreach.loop.last == false},{/if}
{/foreach}
]
As other said, though - encoding the array as JSON in PHP seems much cleaner and easier way to do that.
正如其他人所说,尽管 - 在 PHP 中将数组编码为 JSON 似乎更清晰、更简单。
回答by clem
first create a new javascript array:
首先创建一个新的javascript数组:
var array = new array();
loop through the php array in your tpl and create the javascript array
循环遍历 tpl 中的 php 数组并创建 javascript 数组
foreach($phparray as $i => $ary) {
echo "array[".$i."] = new array(".$ary['dt'].",".$ary['number'].");
}