-1

Am using chart.js to display charts on my site. The working data attribute for chart.js is in this format

"data" => ['1', '2', '3'] 

I have my data currently in a PHP array

Array ( [0] => 1 [1] => 2 [2] => 3 ) 

However this is not working for my needs. How can I convert the PHP array into the same format as above?

I have tried

 json_encode($myArray)

and

implode($myArray)

without success. Any suggestions?

Ajith
  • 1,447
  • 2
  • 17
  • 31
dungey_140
  • 2,602
  • 7
  • 34
  • 68
  • "The working data attribute for chart.js is in this format `"data" => ['1', '2', '3'].`" <- I doubt it, that doesn't look like JS at all. Please share your current JS code (with hardcoded data). – Jeto Nov 14 '19 at 22:28

2 Answers2

0

You could create an array with key "data" and another array as the value;

$myArray = ["data" => [1, 2, 3]];
echo json_encode($myArray);

Output

{"data":[1,2,3]}

Or put the values between quotes

$myArray = ["data" => ["1", "2", "3"]];

Output

{"data":["1","2","3"]}
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • No luck with either of these unfortunately, should be so simple? Any further suggestions? – dungey_140 Nov 14 '19 at 21:17
  • I don't know how you feed the data to a chart. If I look at [this page](https://www.chartjs.org/docs/latest/) there are 2 keys named `data`. Is your data structure correct and are you using the right key? Do you get any errors in the console? – The fourth bird Nov 14 '19 at 21:27
-1

Not sure completely understood the problem. But if you want to convert values of php array to a json you can do something like that: json_encode(array_values($myArray));

Pavel K
  • 9
  • 1