I have a php array that i converted in Js using the json_encode method:
$mesi = ['Nov', 'Dic'];
var js_mesi = '<?php print(json_encode($mesi)); ?>';
this is the converted array
["Nov","Dic"]
The problem I'm having now it's to place the Js array inside a JS object like this
data: {
labels: [js_mesi],
}
because the values of js_mesi are recognized as a single value and not as two separate values. The output I would like to have is this:
data: {
labels: ["Nov", "Dic"]
}
instead I see this
data: {
labels: ["Nov,Dic"]
}
In the javascript variable assignment, you had added '
single quotes around the PHP block.
Hence it was considered as a string and would have got the value '["Nov","Dic"]'
instead of an array.
Corrected code
<?php
$mesi = array('Nov','Dic');
?>
var js_mesi = <?php print(json_encode($mesi)); ?>;
Output
var js_mesi = ["Nov","Dic"];
Refer https://eval.in/920653 for the output