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

Php 将句子单词数组转换为flare.json格式,php,Php,在PHP中,我试图将一些句子转换成嵌套的单词结构,就像某些D3.js树图所用的那样,通常称为flare.json 我把句子作为数组(实际上它们的长度都不一样): $句子=[ [“最好的”、“最好的”、“最好的”], [“最佳”、“最佳”、“地点”], [“那”、“好”、“东西”], [“A”、“好”、“东西”] ]; 并希望最终得到如下嵌套数组结构: $flare=[ [ “name”=>“The”, “儿童”=>[ [ “名称”=>“最佳”, “儿童”=>[ [ “名称”=>“事物” ],

在PHP中,我试图将一些句子转换成嵌套的单词结构,就像某些D3.js树图所用的那样,通常称为
flare.json

我把句子作为数组(实际上它们的长度都不一样):

$句子=[
[“最好的”、“最好的”、“最好的”],
[“最佳”、“最佳”、“地点”],
[“那”、“好”、“东西”],
[“A”、“好”、“东西”]
];
并希望最终得到如下嵌套数组结构:

$flare=[
[ 
“name”=>“The”,
“儿童”=>[
[
“名称”=>“最佳”,
“儿童”=>[
[
“名称”=>“事物”
],
[
“名称”=>“地点”
],
],
],
[ 
“name”=>“不错”,
“儿童”=>[
[ 
“名称”=>“事物”
] 
]
],
] 
],
[ 
“名称”=>“A”,
“儿童”=>[
[
“name”=>“不错”,
“儿童”=>[
[
“名称”=>“事物”
] 
] 
] 
] 
]
];

但是,当我试图找出如何迭代并构建该结构时,我的大脑失败了。

好吧,这项任务很有挑战性,因为我们需要迭代树级别中的每个单词。我写递归的方法来完成这个任务

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;
    } 
}
这就是输出

[
  {
    "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;
    } 
}
这就是输出

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

这个结构背后的逻辑是什么?第一层的元素包含句子的第一个单词,第二层的元素包含第二个单词,等等。这有意义吗?嗯,但是这怎么可能是->`“children”=>[[[“name”=>“thing”],[“name”=>“place”],],`--EDIT:我想我现在知道了。你能运行这段代码看看从一开始就获得一个点对你是否有帮助吗?:
$res=[];foreach($k=>$v){$res[$k]['name']=$v[0];$res[$k]['children'][$v[1];$res k][$children'][$k]['children']]=$v[2];]echo json_encode($res);
-它返回的结果与您想要的结果不同。
[{“姓名”:“The”,“children”:[“best”,“thing”]},{“name”:[“best”,“place”]},{“name”,“thing”]},{“name”:“A”,“children”:[“nice”,“thing”]}]
,但可能有用吗?是的,这就是我所知道的:)它需要
“name”
s表示最深层次,但我也不确定如何使其适用于任意长度的句子。这种结构背后的逻辑是什么?第一级元素包含句子的第一个单词,第二级元素包含第二个单词,等等。这有意义吗?嗯,但这怎么可能是->`“children”=>[[“name”=>“thing”],[“name”=>“place”],],`--EDIT:我想我现在知道了。你能运行这段代码看看从一开始获取一个点是否有帮助吗?:
$res=[];foreach($k=>$v){$res[$k]['name']=$v[0];$res[$k]['children'][['children']]=$v[1];$res[$k]['children']['children']=$v[2];}echo json_encode($res)
-它返回的结果与您想要的结果不同,
[{code>[{name:“The”,“children best”,“thing”],['thing”],{thing”],{孩子们:“[“很好”,“东西”]}]
但是可能会有帮助吗?是的,这就是我所知道的:)它需要
的名字"
s是最深层次的问题,但我也不知道如何使其适用于任意长度的句子。谢谢Surya!这项工作做得很好!我无法理解问题的递归性质。我有一个问题是它卡在循环中。我想如果有两个相同的句子,就会发生这种情况。我想我已经解决了它将以
$flare[$i][“children”]=put_word(…
放在
if(count($sequence)>$level+1){…}开头的行中
检查。谢谢你,苏里亚!这工作做得很好!我无法理解问题的递归性质。我有一个问题是它陷入了循环。我想如果有两个相同的句子,就会发生这种情况。我想我把它改成了以“
$flare[$I][“children”]=put\u word”开头的一行(…
if(count($句子)>$level+1){…}
检查中。