This question already has an answer here:
I've got a problem to pass a var from php to js. BUT, I've already read all the related topics without finding any clue... I've got a js script, which contains the part
data: [
{ dt: '2009', value: 10 },
{ dt: '<?= $vstDate[0] ?>', value: 20 },
{ dt: '2009', value: 10 },
],
Then, what I'd like to do is adding a given number of vars, stored in a php array. I tried this, without getting it to work :
data: [
<?php
for($i=0; $i<=count($vststats)-1; $i+=1) {
echo json_encode("{ dt: ".$vststats[$i].", value: 5 },");
}
?>
],
Please, how could I make it working ? All my thanks in advance !
</div>
First make an array in php and in end pass array to JSON_ENCODE
//Array
$temp = array(array('dt'=>2009,'value'=>10), array('dt'=>2010,'value'=>10))
//json encode
echo json_encode($temp);