How can the set_path function provided in the forum thread be optimized for creating a dynamic organigram structure in PHP?

The set_path function can be optimized for creating a dynamic organigram structure in PHP by using recursion to traverse the hierarchy of the organigram and build the path dynamically. This approach allows for flexibility in handling different levels of the organigram and ensures that the path is correctly constructed for each node.

function set_path($id, $path = '') {
    $node = get_node_by_id($id); // Function to retrieve node data by ID
    $path = $node['name'] . '/' . $path;

    if ($node['parent_id'] != 0) {
        $path = set_path($node['parent_id'], $path);
    }

    return $path;
}

// Example usage
$organigram_path = set_path($node_id);
echo $organigram_path;