When working with complex array structures in PHP, what are some alternative methods to iterate through all levels of the array efficiently?

When working with complex array structures in PHP, one alternative method to efficiently iterate through all levels of the array is by using recursion. Recursion allows you to traverse through nested arrays without knowing the depth of the structure beforehand. By creating a recursive function that calls itself for each nested array, you can effectively iterate through all levels of the array.

function iterateArray($array) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            iterateArray($value);
        } else {
            echo $key . ': ' . $value . "\n";
        }
    }
}

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

iterateArray($array);