What are the best practices for handling unknown levels of recursion in PHP array structures?

When dealing with unknown levels of recursion in PHP array structures, it is best to use a recursive function to traverse the array and handle each level dynamically. This allows you to handle arrays of any depth without needing to know the exact structure beforehand. By recursively calling the function on nested arrays, you can effectively handle any level of recursion.

function handleUnknownRecursion($array) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            handleUnknownRecursion($value);
        } else {
            // Handle leaf node value
            echo $value . "\n";
        }
    }
}

// Example usage
$array = [
    'key1' => 'value1',
    'key2' => [
        'subkey1' => 'subvalue1',
        'subkey2' => [
            'subsubkey1' => 'subsubvalue1'
        ]
    ]
];

handleUnknownRecursion($array);