Home > database >  How to get GoJS mindmap JSON data into path variable in PHP
How to get GoJS mindmap JSON data into path variable in PHP

Time:10-06

I am using gojs to make a mind map. What I want to do is, to format the JSON output into paths. I mean I want to take paths of the mindmap like this.

root > node1 > node2 > node3

Here is the output 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"}]}

Can anyone give a help with this in PHP.

CodePudding user response:

For this type of problem, I can imagine that it's not easy to start off if you're learning programming. To solve this kind of problem in PHP, I would first convert the JSON to a PHP variable then build myself a dictionnary (kind of array with keys instead of contigus numerical indexes). This will help you have quick access to the data, typically to find the parents of a node entry.

You can also use recursion to get the parents of the parents, etc...

Solution

<?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 and it will output this:

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

  • Related