Converting PHP XPathNodeList to javascript array?
I have a small PHP script I have written that gets reviews from a website. However, I am trying to display them in Javascript but cannot get the values through to the JS.
in PHP I have the variable $reviewTitles populated using:
$reviewTitles = $xpath->query("//*[@class='" . $reviewStr . "']");
and I can access each value in PHP by using
$title = $reviewTitles->item($x)->nodeValue;
Where $x is the index of the title to display.
My question is how can I convert whatever object the php variable $reviewTitles holds into javascript, So I am able to access each title via a JavaScript array.
I have tried:
<script language="JavaScript" type="text/javascript">
var titles = '<?php echo json_encode($reviewTitles); ?>';
var titles_data = JSON.parse(titles );
window.alert(titles_data[0]);
</script>
But get nothing - What am I doing wrong?
Thanks for taking the time to read this.