What are some common methods for organizing and outputting hierarchical data structures in PHP?

When working with hierarchical data structures in PHP, it is common to organize the data in a way that allows for easy traversal and output. One common method to achieve this is by using recursive functions to iterate through the nested data and output it in a hierarchical format. Here is an example of how you can organize and output hierarchical data structures in PHP using a recursive function:

<?php

// Sample hierarchical data structure
$data = [
    'name' => 'Parent',
    'children' => [
        [
            'name' => 'Child 1',
            'children' => [
                [
                    'name' => 'Grandchild 1',
                    'children' => []
                ],
                [
                    'name' => 'Grandchild 2',
                    'children' => []
                ]
            ]
        ],
        [
            'name' => 'Child 2',
            'children' => []
        ]
    ]
];

// Recursive function to output hierarchical data
function outputHierarchy($data, $level = 0) {
    $indent = str_repeat('    ', $level);
    
    echo $indent . $data['name'] . PHP_EOL;
    
    foreach ($data['children'] as $child) {
        outputHierarchy($child, $level + 1);
    }
}

// Output hierarchical data
outputHierarchy($data);

?>