To make a PHP array available to JavaScript (js), first have to encode it in json. Then pass the json object to Javascript.

Its important NOT to include any quote mark while defining the js variable.

<?php
$jsonContent = json_encode($my_php_array);
get_header();
echo '<script type="text/javascript">
    var dataObj = '. $jsonContent.';
    </script>';
?>

 

Comments

comments