Can anyone tell me the best way to pass a PHP array into a Javascript array? It just crossed my mind just now. I'm not too familiar wit JSON Or XML but here's what I have so far but it doesn't work though. What's wrong with it? var js_arr = []; <?php $tempo_arr = array('list', 'of', 'elements'); foreach($tempo_arr as $element) { echo "js_arr.push($element);\n"; } ?> alert(js_arr); Code (markup):
You should probably do something like: echo "js_arr.push('$element');\n"; PHP: Or if you have PHP 5.2+, you can just do: echo 'js_arr = ' . json_encode($temp_arr);