如何将 GoJS 思维导图 JSON 数据放入 PHP 中的路径变量
How to get GoJS mindmap JSON data into path variable in PHP
我正在用gojs做思维导图。我想要做的是,将 JSON 输出格式化为路径。我的意思是我想像这样走思维导图的路径。
root > 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 中提供帮助。
对于这类问题,我可以想象,如果你是学编程的,是很难上手的。为了解决 PHP 中的此类问题,我首先将 JSON 转换为 PHP 变量,然后自己构建一个 dictionnary (一种数组使用键而不是连续的数字索引)。这将帮助您快速访问数据,通常是查找节点条目的 parents。
你也可以使用递归得到parents的parents,等等...
解决方案
<?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";
}
You can run it here 它会输出这个:
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
我正在用gojs做思维导图。我想要做的是,将 JSON 输出格式化为路径。我的意思是我想像这样走思维导图的路径。
root > 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 中提供帮助。
对于这类问题,我可以想象,如果你是学编程的,是很难上手的。为了解决 PHP 中的此类问题,我首先将 JSON 转换为 PHP 变量,然后自己构建一个 dictionnary (一种数组使用键而不是连续的数字索引)。这将帮助您快速访问数据,通常是查找节点条目的 parents。
你也可以使用递归得到parents的parents,等等...
解决方案
<?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";
}
You can run it here 它会输出这个:
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