如何将GoJS思维导图JSON数据转换为PHP中的路径变量



我正在用gojs制作思维导图。我要做的是,将JSON输出格式化为路径。我的意思是,我想把思维导图的路径画成这样。

根比;node1祝辞node2祝辞node3

输出JSON

{ "class": "TreeModel", "nodeDataArray": [{"key":0,"text":"Mind Map Root","loc":"-323 -89","scale":1.2100000000000002,"font":"bold 13px sans-serif"},{"text":"parent1","brush":"#f9a281","parent":0,"key":-14,"loc":"-160.293935546875 -179.425","dir":"right"},{"text":"parent1child1","brush":"#f9a281","parent":-14,"key":-15,"loc":"-66.20311523437499 -220.425","dir":"right"},{"text":"parent1child2","brush":"#f9a281","parent":-14,"key":-16,"loc":"-66.20311523437499 -179.425","dir":"right"},{"text":"parent1child3","brush":"#f9a281","parent":-14,"key":-17,"loc":"-66.20311523437499 -138.425","dir":"right"},{"text":"parent1child1child1","brush":"#f9a281","parent":-15,"key":-18,"loc":"61.85401367187501 -240.925","dir":"right","font":"bold 13px sans-serif"},{"text":"parent1child1child2","brush":"#f9a281","parent":-15,"key":-19,"loc":"61.85401367187501 -199.925","dir":"right"},{"text":"parent2","brush":"#a2e2f7","parent":0,"key":-9,"loc":"-160.293935546875 -66.92500000000001"},{"text":"parent2child1","brush":"#a2e2f7","parent":-9,"key":-10,"loc":"-66.20311523437499 -107.92500000000001"},{"text":"parent2child1child1","brush":"#a2e2f7","parent":-10,"key":-11,"loc":"61.85401367187501 -128.425"},{"text":"parent2child2","brush":"#a2e2f7","parent":-9,"key":-12,"loc":"-66.20311523437499 -25.92500000000001"},{"text":"parent2child1child2","brush":"#a2e2f7","parent":-10,"key":-13,"loc":"61.85401367187501 -87.42500000000001"},{"text":"parent2child2child1","brush":"#a2e2f7","parent":-12,"key":-20,"loc":"61.85401367187501 -46.42500000000001"},{"text":"parent2child2child2","brush":"#a2e2f7","parent":-12,"key":-21,"loc":"61.85401367187501 -5.425000000000011"},{"text":"parent3","brush":"#c2ace6","parent":0,"key":-22,"loc":"-160.293935546875 -25.92500000000001"},{"text":"parent4","brush":"#f49ffa","dir":"left","parent":0,"key":-23,"loc":"-467.29393554687505 -56.92500000000001"},{"text":"idea","brush":"#f9a281","dir":"right","parent":-18,"key":-24,"loc":"223.877451171875 -240.925"}]}

有谁能帮我一下吗?

对于这种类型的问题,我可以想象,如果您正在学习编程,那么开始是不容易的。为了在PHP中解决这类问题,我首先将JSON转换为PHP变量,然后自己构建一个字典(一种带有键而不是连续数字索引的数组)。这将帮助您快速访问数据,通常是查找节点条目的父节点。

也可以使用递归要得到父母的父母等……

解决方案
<?php
$mindmap_json = <<<JSON
{ "class": "TreeModel",  "nodeDataArray": [{"key":0,"text":"Mind Map Root","loc":"-323 -89","scale":1.2100000000000002,"font":"bold 13px sans-serif"},{"text":"parent1","brush":"#f9a281","parent":0,"key":-14,"loc":"-160.293935546875 -179.425","dir":"right"},{"text":"parent1child1","brush":"#f9a281","parent":-14,"key":-15,"loc":"-66.20311523437499 -220.425","dir":"right"},{"text":"parent1child2","brush":"#f9a281","parent":-14,"key":-16,"loc":"-66.20311523437499 -179.425","dir":"right"},{"text":"parent1child3","brush":"#f9a281","parent":-14,"key":-17,"loc":"-66.20311523437499 -138.425","dir":"right"},{"text":"parent1child1child1","brush":"#f9a281","parent":-15,"key":-18,"loc":"61.85401367187501 -240.925","dir":"right","font":"bold 13px sans-serif"},{"text":"parent1child1child2","brush":"#f9a281","parent":-15,"key":-19,"loc":"61.85401367187501 -199.925","dir":"right"},{"text":"parent2","brush":"#a2e2f7","parent":0,"key":-9,"loc":"-160.293935546875 -66.92500000000001"},{"text":"parent2child1","brush":"#a2e2f7","parent":-9,"key":-10,"loc":"-66.20311523437499 -107.92500000000001"},{"text":"parent2child1child1","brush":"#a2e2f7","parent":-10,"key":-11,"loc":"61.85401367187501 -128.425"},{"text":"parent2child2","brush":"#a2e2f7","parent":-9,"key":-12,"loc":"-66.20311523437499 -25.92500000000001"},{"text":"parent2child1child2","brush":"#a2e2f7","parent":-10,"key":-13,"loc":"61.85401367187501 -87.42500000000001"},{"text":"parent2child2child1","brush":"#a2e2f7","parent":-12,"key":-20,"loc":"61.85401367187501 -46.42500000000001"},{"text":"parent2child2child2","brush":"#a2e2f7","parent":-12,"key":-21,"loc":"61.85401367187501 -5.425000000000011"},{"text":"parent3","brush":"#c2ace6","parent":0,"key":-22,"loc":"-160.293935546875 -25.92500000000001"},{"text":"parent4","brush":"#f49ffa","dir":"left","parent":0,"key":-23,"loc":"-467.29393554687505 -56.92500000000001"},{"text":"idea","brush":"#f9a281","dir":"right","parent":-18,"key":-24,"loc":"223.877451171875 -240.925"}]}
JSON;
$mindmap = json_decode($mindmap_json);
// var_export($mindmap);
// The dictionnary for quick access by key.
$nodes = [];
foreach ($mindmap->nodeDataArray as $i => $node) {
$nodes[$node->key] = $node;
}
/**
* Recursive function to get the path to a node.
* 
* @param int $node_id The node id you want to get the path.
* @param array $nodes The array of nodes you want to work on.
* @param string $separator The separator between node titles.
*/
function getPath($node_id, array &$nodes, $separator = " > ") {
if (!isset($nodes[$node_id])) {
throw new Exception("Node id $node_id doesn't exist!");
}
if (!isset($nodes[$node_id]->parent)) {
return $nodes[$node_id]->text;
} else {
return getPath($nodes[$node_id]->parent, $nodes, $separator) .
$separator . $nodes[$node_id]->text;
}
}
// Print the path of each node.
foreach ($nodes as $node_id => $node) {
print getPath($node_id, $nodes) . "n";
}

你可以在这里运行它,它将输出如下:

Mind Map Root
Mind Map Root > parent1
Mind Map Root > parent1 > parent1child1
Mind Map Root > parent1 > parent1child2
Mind Map Root > parent1 > parent1child3
Mind Map Root > parent1 > parent1child1 > parent1child1child1
Mind Map Root > parent1 > parent1child1 > parent1child1child2
Mind Map Root > parent2
Mind Map Root > parent2 > parent2child1
Mind Map Root > parent2 > parent2child1 > parent2child1child1
Mind Map Root > parent2 > parent2child2
Mind Map Root > parent2 > parent2child1 > parent2child1child2
Mind Map Root > parent2 > parent2child2 > parent2child2child1
Mind Map Root > parent2 > parent2child2 > parent2child2child2
Mind Map Root > parent3
Mind Map Root > parent4
Mind Map Root > parent1 > parent1child1 > parent1child1child1 > idea

最新更新