$line) { //name our node $node[$key]['name'] = $key; //setup position data for our root node if($key == 0) { $node[$key]['xPos'] = 200; $node[$key]['yPos'] = 50; $node[$key]['isSelected'] = true; } $items = explode(',', $line); //iterate over each dataitem from our line foreach($items as $itemKey => $item) { //this is a child of $key if($item == 1) { //if childCount equals 1 we have encountered the first childNode if($childCount == 1) { $node[$key]['leftChild'] = $itemKey; $node[$itemKey]['xPos'] = $node[$key]['xPos'] - 50; $node[$itemKey]['yPos'] = $node[$key]['yPos'] + 25; } else { $node[$key]['rightChild'] = $itemKey; $node[$itemKey]['xPos'] = $node[$key]['xPos'] + 50; $node[$itemKey]['yPos'] = $node[$key]['yPos'] + 25; } //ensure that none of our nodes (with the exception of root) are marked as selected $node[$itemKey]['isSelected'] = false; $childCount++; } } $childCount = 1; } echo json_encode($node); ?>