assigning multidimensional php array to javascript array

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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..

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

  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



回答5:

<?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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!