将多维 php 数组分配给 javascript 数组

发布于 2024-12-14 07:46:42 字数 847 浏览 1 评论 0原文

我知道这可能是重复的,但我无法理解其他示例。如有帮助,将不胜感激。 我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倥絔 2024-12-21 07:46:42

您不能使用整数作为变量名称,如以下行所示:print "var 25[".$i."]=" .$result1['25'].";\n";。 25 不能是变量。

如果您想将数组映射到 JavaScript 对象,您可能需要查看 json_encode

示例
你的代码可以这样写:

<?php
$result = array();

while ($row = mysql_fetch_array($query1)){
  $result[] = $row;
}
?>
<script>
  var result = <?= json_encode($result); ?>;
  alert (result[1][500]);
</script>

对我来说看起来更干净。

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:

<?php
$result = array();

while ($row = mysql_fetch_array($query1)){
  $result[] = $row;
}
?>
<script>
  var result = <?= json_encode($result); ?>;
  alert (result[1][500]);
</script>

looks much cleaner to me.

記柔刀 2024-12-21 07:46:42

您使用数组的方式不正确。

首先你应该初始化数组:

var myArr = [];

然后如果你只想添加到数组中,你可以使用push:

myArr.push("something");

或到特定索引:

myArr[11] = "something";

你使用的语法是完全无效的。

The way you are working with arrays is not correct.

First you should initialize the array:

var myArr = [];

Then if you just want to add to the array, you can use push:

myArr.push("something");

or to a specific index:

myArr[11] = "something";

The syntax you are using is completely invalid.

懒猫 2024-12-21 07:46:42

由于 PHP 生成的内容,您的代码是错误的(特别是因为您在 JavaScript 中使用数字作为变量名,而且您在每个循环中定义了相同的变量)。

为了简化您想要实现的目标,只需在 PHP 中创建一些变量并为其赋值。让我们称之为例如。 $my_proxy_var

然后像这样将其传递给 JavaScript(在某些

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

只需记住:

  • PHP 中的非关联数组在 JavaScript 中变为简单数组,而
  • 在 JavaScript 中则变为关联数组PHP 在 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):

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

Just remember that:

  • non-associative array in PHP becomes simple array in JavaScript,
  • associative array in PHP becomes object in JavaScript,

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.

若能看破又如何 2024-12-21 07:46:42
  1. 在 JavaScript 中不能使用数字作为变量名。
  2. 您不需要在每一行中使用“var”。类似的东西

    var test = [];
    test[1] = '某个值';
    test[2] = '某个值';
    
  3. 您可能想查看使用 JSON_ENCODE 函数来自 PHP

  1. You can't use numbers as variable names in javascript.
  2. You don't need to use "var" with each line. Something like

    var test = [];
    test[1] = 'some value';
    test[2] = 'some value';
    
  3. You probably want to look at using the JSON_ENCODE function from PHP

剩一世无双 2024-12-21 07:46:42
<?php
    if (!func_exists('json_encode')) die('sorry... I tried');
    $buffer = array();
    while ($value = mysql_fetch_assoc($result)) {
        $buffer[] = $value;
    }
    echo "<script>var data = ".json_encode($buffer)."</script>";
?>
<script>
console.log(data);
</script>

需要 PHP 5.2.0

<?php
    if (!func_exists('json_encode')) die('sorry... I tried');
    $buffer = array();
    while ($value = mysql_fetch_assoc($result)) {
        $buffer[] = $value;
    }
    echo "<script>var data = ".json_encode($buffer)."</script>";
?>
<script>
console.log(data);
</script>

Requires PHP 5.2.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文