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

share|improve this question
actually, this can be fairly simply using json_encode($array), available since PHP 5.2.0 – andrewjackson Nov 7 '11 at 22:21
feedback

5 Answers

up vote 2 down vote accepted

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.

share|improve this answer
AWESOME!!! This does it beautifully! I just need to study json_encode. Thank you for the help! – Daniel Hunter Nov 7 '11 at 22:29
feedback

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.

share|improve this answer
Thank You. I am still Learning and what you are saying makes sense. – Daniel Hunter Nov 7 '11 at 22:33
feedback

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.

share|improve this answer
and json_encode can be overridden: php.net/manual/en/function.json-encode.php – Jim H. Nov 7 '11 at 22:23
@JimH.: What do you mean by " and json_encode can be overridden "? According to the documentation you can override built-in functions, but what is the point? – Tadeck Nov 7 '11 at 22:31
feedback
  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

share|improve this answer
feedback
<?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

share|improve this answer
doh! someone beat me – andrewjackson Nov 7 '11 at 22:29
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.