将数组句子单词转换为flare.json格式。

在PHP中,我试图将一些句子转换为嵌套结构的单词,就像用于一些D3.js树状图一样。诸如此类俗称 flare.json.

我把这些句子作为一个数组(实际上它们的长度都会不同)。

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

我想最终得到一个像这样的嵌套数组结构。

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

但是,当我试图找出如何迭代和构建该结构时,我的大脑却很不灵光。

解决方案:

好吧,这个任务很有挑战性,因为我们需要迭代每一个词在他们的树的水平。我写递归的方式来完成这个任务

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;
    } 
}

所以你可以直接用数组单词调用函数

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

$flare = [];

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

这就是输出

[
  {
    "name": "The",
    "children": [
      {
        "name": "best",
        "children": [
          {
            "name": "thing"
          },
          {
            "name": "place"
          }
        ]
      },
      {
        "name": "nice",
        "children": [
          {
            "name": "thing"
          }
        ]
      }
    ]
  },
  {
    "name": "A",
    "children": [
      {
        "name": "nice",
        "children": [
          {
            "name": "thing"
          }
        ]
      }
    ]
  }
]

给TA打赏
共{{data.count}}人
人已打赏
解决方案

如何在Dataproc中访问SparkContext?

2022-5-11 16:00:15

解决方案

Azure Devops 发布测试结果任务 - 测试选项卡屏幕截图

2022-5-11 16:00:17

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索