Skip to content
Advertisement

Convert array of array of sentence words into flare.json format

In PHP I’m trying to convert some sentences into a nested structure of words, like that used for some D3.js tree diagrams, such as this, often called flare.json.

I have the sentences as an array of arrays (in reality they’d all be different lengths):

JavaScript

And want to end up with a nested array structure like this:

JavaScript

But my brain is failing when I try to work out how to iterate through and construct that structure.

Advertisement

Answer

Okay this task in challenging because we need to iterate every word in their level of tree. I write recursive way to do this task

JavaScript

so you can just call the function with array of words

JavaScript

and this is the output

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