将多维 php 数组分配给 javascript 数组
我知道这可能是重复的,但我无法理解其他示例。如有帮助,将不胜感激。 我有一个 php 数组,需要将其分配给 javascript 数组。这是我现在的业余做法。 您可以在 http://www.preferweb.com/accentps/index.php 查看源
<?php
$i=0;
while ($result1 = mysql_fetch_array($query1)){
print "<script>";
print "var size[".$i."]=" .$result1['type'].";\n";
print "var 25[".$i."]=" .$result1['25'].";\n";
print "var 50[".$i."]=" .$result1['50'].";\n";
print "var 100[".$i."]=" .$result1['100'].";\n";
print "var 250[".$i."]=" .$result1['250'].";\n";
print "var 500[".$i."]=" .$result1['500'].";\n";
print "var plus[".$i."]=" .$result1['plus'].";\n";
$i = $i+1;
}
print "var tick='1';\n";
print "alert (tick);\n";
print "</script>\n";
?>
<script>
alert (500[0]);
</script>
代码勾选警报未定义警报,第二个警报没有任何警报..谢谢..
I know this may be a duplicate, but I cant wrap my brain around the other examples. Help would be appreciated.
I have a php array that i need to assign to a javascript array. Here is my amateur way of doing it now.
You can see source at http://www.preferweb.com/accentps/index.php
<?php
$i=0;
while ($result1 = mysql_fetch_array($query1)){
print "<script>";
print "var size[".$i."]=" .$result1['type'].";\n";
print "var 25[".$i."]=" .$result1['25'].";\n";
print "var 50[".$i."]=" .$result1['50'].";\n";
print "var 100[".$i."]=" .$result1['100'].";\n";
print "var 250[".$i."]=" .$result1['250'].";\n";
print "var 500[".$i."]=" .$result1['500'].";\n";
print "var plus[".$i."]=" .$result1['plus'].";\n";
$i = $i+1;
}
print "var tick='1';\n";
print "alert (tick);\n";
print "</script>\n";
?>
<script>
alert (500[0]);
</script>
This alerts undefined for the tick alert and nothing for the second alert.. Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能使用整数作为变量名称,如以下行所示:
print "var 25[".$i."]=" .$result1['25'].";\n";
。 25 不能是变量。如果您想将数组映射到 JavaScript 对象,您可能需要查看 json_encode
示例
你的代码可以这样写:
对我来说看起来更干净。
You cannot use an integer as a variable name, like in this line:
print "var 25[".$i."]=" .$result1['25'].";\n";
. 25 cannot be a variable.If you want to map an array to a javascript object, you might want to take a look at json_encode
EXAMPLE
Your code could be written like this:
looks much cleaner to me.
您使用数组的方式不正确。
首先你应该初始化数组:
然后如果你只想添加到数组中,你可以使用push:
或到特定索引:
你使用的语法是完全无效的。
The way you are working with arrays is not correct.
First you should initialize the array:
Then if you just want to add to the array, you can use push:
or to a specific index:
The syntax you are using is completely invalid.
由于 PHP 生成的内容,您的代码是错误的(特别是因为您在 JavaScript 中使用数字作为变量名,而且您在每个循环中定义了相同的变量)。
为了简化您想要实现的目标,只需在 PHP 中创建一些变量并为其赋值。让我们称之为例如。
$my_proxy_var
。然后像这样将其传递给 JavaScript(在某些
标记内):
只需记住:
这一点很重要,这样您就可以避免混淆,并在每个级别上在非关联数组和关联数组之间进行选择。
您可以在此键盘上测试代码。
Your code is wrong because of what is generated by PHP (especially because you use numbers as variable names in JavaScript, plus you define the same variables with each loop).
To simplify what you want to achieve, just create some variable in PHP and assign a value to it. Lets call it eg.
$my_proxy_var
.Then pass it to JavaScript like that (within some
<script>
tag):Just remember that:
This is important so you can avoid confusion and chose between non-associative and associative array on each level.
You can test the code on this codepad.
您不需要在每一行中使用“var”。类似的东西
您可能想查看使用 JSON_ENCODE 函数来自 PHP
You don't need to use "var" with each line. Something like
You probably want to look at using the JSON_ENCODE function from PHP
需要 PHP 5.2.0
Requires PHP 5.2.0