How to send a PHP array to JS object in WordPress
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>';
?>
Leave a Reply
You must be logged in to post a comment.