You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

实现思路
  • 先解析GoJS输出的JSON数据,提取nodeDataArray节点数组
  • 把所有节点转换为key为索引的关联数组,方便后续快速查找父节点信息
  • 遍历每个节点,从当前节点向上递归查找父节点,直到找到没有parent属性的根节点
  • 把查找得到的层级节点文本按顺序拼接,就得到完整的节点路径
PHP 实现代码
<?php
// GoJS输出的JSON字符串
$gojsJson = '{ "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为数组
$gojsData = json_decode($gojsJson, true);
$nodeList = $gojsData['nodeDataArray'];

// 建立key为索引的节点映射,方便快速查找
$nodeMap = [];
foreach ($nodeList as $node) {
    $nodeMap[$node['key']] = $node;
}

// 生成单个节点的完整路径
function getNodePath($nodeKey, $nodeMap) {
    $pathParts = [];
    $currentKey = $nodeKey;
    // 向上遍历直到没有父节点
    while (isset($nodeMap[$currentKey])) {
        $currentNode = $nodeMap[$currentKey];
        array_unshift($pathParts, $currentNode['text']);
        // 没有父节点则终止循环
        if (!isset($currentNode['parent'])) break;
        $currentKey = $currentNode['parent'];
    }
    return implode(' > ', $pathParts);
}

// 生成所有节点的路径
$allPathList = [];
foreach ($nodeMap as $nodeKey => $node) {
    $allPathList[] = getNodePath($nodeKey, $nodeMap);
}

// 输出结果
print_r($allPathList);
?>
输出示例

运行上述代码后会得到所有节点的路径,部分结果如下:

  • Mind Map Root
  • Mind Map Root > parent1
  • Mind Map Root > parent1 > parent1child1
  • Mind Map Root > parent1 > parent1child1 > parent1child1child1
  • Mind Map Root > parent1 > parent1child1 > parent1child1child1 > idea
  • Mind Map Root > parent2 > parent2child1 > parent2child1child1

内容的提问来源于stack exchange,提问作者Harsha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 03:39:02