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):

$sentences = [
    ["The", "best", "thing"],
    ["The", "best", "place"],
    ["The", "nice", "thing"],
    ["A", "nice", "thing"]
];

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

$flare = [ 
    [ 
        "name" => "The",
        "children" => [
            [
                "name" => "best",
                "children" => [
                    [
                        "name" => "thing"
                    ],
                    [
                        "name" => "place"
                    ],
                ],
            ],
            [ 
                "name" => "nice",
                "children" => [ 
                    [ 
                        "name" => "thing"
                    ] 
                 ]
            ],
        ] 
    ],
    [ 
        "name" => "A",
        "children" => [
            [
                "name" => "nice",
                "children" => [
                    [
                        "name" => "thing"
                    ] 
                ] 
            ] 
        ] 
    ]
];

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

function put_word($flare, $sentence, $level) {
    // check if there are name exists
    for($i = 0; $i<count($flare); $i++) {
        if($flare[$i]["name"] == $sentence[$level]) {
            // check if this is last word
            if($level == count($sentence)-1) {
                return $flare;
            }
            // if found, add their children
            if(!array_key_exists("children", $flare[$i])) {
                $flare[$i]["children"] = [];
            }
            $flare[$i]["children"] = put_word($flare[$i]["children"], $sentence, $level + 1);
            return $flare;
        }
    }
    // if not found, add new array
    $flare[] = [
        "name" => $sentence[$level],
    ];
    $last = count($flare) - 1;
    // stop criteria
    if($level == count($sentence) - 1) {
        return $flare;
    } else {
        $flare[$last]["children"] = put_word([], $sentence, $level + 1);
        return $flare;
    } 
}

so you can just call the function with array of words

$sentences = [
    ["The", "best", "thing"],
    ["The", "best", "place"],
    ["The", "nice", "thing"],
    ["A", "nice", "thing"]
];

$flare = [];

foreach($sentences as $sentence) {
    $flare = put_word($flare, $sentence, 0);
}

and this is the output

[
  {
    "name": "The",
    "children": [
      {
        "name": "best",
        "children": [
          {
            "name": "thing"
          },
          {
            "name": "place"
          }
        ]
      },
      {
        "name": "nice",
        "children": [
          {
            "name": "thing"
          }
        ]
      }
    ]
  },
  {
    "name": "A",
    "children": [
      {
        "name": "nice",
        "children": [
          {
            "name": "thing"
          }
        ]
      }
    ]
  }
]
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement