Skip to content
Advertisement

d3.js – PHP Array format

I need to create a d3.js chart using PHP formatted array for JSON. The format is like:

JavaScript

The array code and format that I currently have is below.

The format of the array clearly needs to be changed, but I’m not sure how to do that to suite the above format.

JavaScript

Advertisement

Answer

The best approach may be to create your $data array in the desired format from the onset as this would be more efficient and be less code. See below:

JavaScript

However, I am not sure if you are using $data as is elsewhere. The code below will allow you to transform your current data to your desired d3jsFormattedData using array_keys.

JavaScript

NB. [] is shorthand for array() , you may read more here on php arrays

Let me know if this works for you.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement