How can you improve the efficiency of nested loops in PHP when dealing with complex data structures?

When dealing with complex data structures in nested loops in PHP, one way to improve efficiency is by minimizing the number of iterations through the inner loops. This can be achieved by restructuring the data or using associative arrays to access elements directly instead of looping through the entire structure each time.

// Example of using associative arrays to improve efficiency of nested loops
$data = [
    'key1' => ['a', 'b', 'c'],
    'key2' => ['x', 'y', 'z']
];

foreach ($data as $key => $values) {
    echo "Key: $key\n";
    foreach ($values as $value) {
        echo "Value: $value\n";
    }
}