简体   繁体   中英

assigning multidimensional php array to javascript array

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

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.

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 .

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.

  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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM