Chartjs-Charts labels and data with php arrays

1👍

It is because $labels is not Javascript, but PHP variable. So what you need to do is to decode it in php part of your code:

...
fclose($f); 
$labels = json_decode($label);

And in Javascript part:

var labels = JSON.parse("<?php echo $labels; ?>");

As part of suggestions of improvement goes, you should not declare global variables. So what you are doing here:

$array3 = [1,2,3,4,5,5,41]; 
$array4 = [1,2,3,4,5,5,41];

Should be:

var array3 = [1,2,3,4,5,5,41]; 
var array4 = [1,2,3,4,5,5,41];

I removed $ – dollars from their names, because it’s common practice to declare jQuery wrapper variables and those are obviously not. Also, by $ prefix to their names, I can only guess that what you wanted to do was to declare php variables inside your <script> and then use them directly, but it is not possible, because first of all, you are missing <?php part, and second – they would be only saved in the memory by just declaring them.

Leave a comment