How can PHP be used to dynamically display and manipulate a hierarchical tree structure like an organizational chart?

To dynamically display and manipulate a hierarchical tree structure like an organizational chart in PHP, you can use recursive functions to traverse the tree and generate the necessary HTML markup for each node. By storing the tree structure in a nested array or database table, you can easily retrieve and display the data in a hierarchical manner.

<?php
// Sample hierarchical tree structure
$org_chart = array(
    'CEO' => array(
        'CFO' => array(
            'Accountant',
            'Financial Analyst'
        ),
        'CTO' => array(
            'Lead Developer',
            'UI/UX Designer'
        )
    )
);

// Recursive function to display organizational chart
function displayOrgChart($org_chart) {
    echo '<ul>';
    foreach($org_chart as $key => $value) {
        echo '<li>' . $key;
        if(is_array($value)) {
            displayOrgChart($value);
        }
        echo '</li>';
    }
    echo '</ul>';
}

// Display the organizational chart
displayOrgChart($org_chart);
?>