Pass array to Javascript

DescriptionPHP snippet by - 09/05/10

Use this simple function to pass a php array to Javascript. Be warned, php supports associative arrays and Javascript does not, so keep the arrays as simple single dimentional arrays and let the functions assign the default array key ids.

Tags

$n = array('test','test2');
 echo '<script type="text/javascript">';
 echo 'var n = new Array("', join($n,'","'), '");';
 echo 'alert(n[1]);</script>';
  • Share
Submitted by: Adam J Nowak
http://hyperspatial.com

Comments and Feedback