Need to convert JSON object to JSON Array for Highcharts

advertisements

I am having so much confusion on this one. Highcharts has a specific format it accepts data in the series. It has to be like this for example:

[ { name: 'Title Here', data: [1,2,3,4,5] } ]

My issues though is, my ajax in php using json_encode() is converting the entire array of data I'm sending back to an js object like this:

 $data = [
       'name' => 'Percent',
       'data' => [1,2,3,4]
       ];
return json_encode($data);

// Returned Result
{name: "Percent", data: {0: 1, 1: 2, 2: 3, 4: 4}}

This is causing issue with the chart in rendering the returned data. How would I convert the returned data to the first and correct way? I'm lost on how to complete this.


$data = [[
       'name' => 'Percent',
       'data' => [1,2,3,4]
       ]];
return json_encode($data);

In JavaScript has no native associative arrays, your array ['name'=>...] translates to object when you apply json_encode() to it.