English 中文(简体)
分配多维的营地阵列,以 j
原标题: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 ].";
";
print "var 25[".$i."]=" .$result1[ 25 ].";
";
print "var 50[".$i."]=" .$result1[ 50 ].";
";
print "var 100[".$i."]=" .$result1[ 100 ].";
";
print "var 250[".$i."]=" .$result1[ 250 ].";
";
print "var 500[".$i."]=" .$result1[ 500 ].";
";
print "var plus[".$i."]=" .$result1[ plus ].";
";
$i = $i+1;
}
print "var tick= 1 ;
";
print "alert (tick);
";
print "</script>
";
?>
<script>
alert (500[0]);

</script>

这种警示没有为警示和第二次警示作出任何规定。 感谢。

最佳回答

You cannot use an integer as a variable name, like in this line: print "var 25[".$i."]=" .$result1[ 25 ]."; ";. 25 cannot be a variable.

如果你想把一个阵列描绘成一个javascript物体,你可能会想看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>

看起来对我来说更清洁。

问题回答

你与阵列合作的方式是不正确的。

首先,你应首先确定阵列:

var myArr = [];

那么,如果你只是想增加阵列,你可以推动:

myArr.push("something");

或具体指数:

myArr[11] = "something";

你使用的辛子完全无效。

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





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.