0

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.

4
  • 1
    What's json_parse()? Did you mean json_encode()? Commented Sep 11, 2015 at 16:34
  • 2
    Also, there is no such thing as a "JSON Object" or a "JSON Array". JSON is a string representation of data (like XML). If it's not a string, it's not JSON. Commented Sep 11, 2015 at 16:35
  • Yes, I meant encode. And I understand that. It's been a long day and my terms are way off. I use Laravel 5 so technically I'm using Response::json([ ]); which is the exact same thing as json_encode. Commented Sep 11, 2015 at 16:45
  • @RocketHazmat Of course JSON has objects and arrays. They're explicitly called out as data types in the spec. Yes, the format itself is text-based, but so is source code for programming languages. Would you say that .java files or .php files are just giant strings? Commented Sep 11, 2015 at 17:23

2 Answers 2

1
$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.

Sign up to request clarification or add additional context in comments.

7 Comments

Why should the OP "just"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
This was it. I apologize. It's been an insanely long week for me and my mind is kinda fried.
@JayBlanchard see update, sorry about language, its not native to me : )
JS does have "native associative arrays" (i.e. Objects). They're just not called that (and unlike PHP they're not interchangeable with numerically indexed arrays).
No, it's a matter of fact. To all intents and purposes a JS object is an associative array per the normal definition. PHP's equivalent is unusual in having the additional feature of supporting numeric indices at the same time.
|
1

The format that Highcharts requires is a JavaScript Array containing an Object.

PHP's json_encode function will encode a plain array as the former, but an associative array with string keys will automatically be encoded as the latter. You therefore just need to wrap your existing data in one more layer of array:

$data = [
  [
    'name' => 'Percent',
    'data' => [1, 2, 3, 4]
  ]
];

I note however that you've said that the plain array [1, 2, 3, 4] appears to have converted into the object {0:1, 1:2, 2:3, 3: 4}.

This is not standard PHP json_encode behaviour, and is perhaps a quirk of the Laravel Response::json([ ]) call you're really using?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.