What are common pitfalls when using nested foreach loops in PHP?

Common pitfalls when using nested foreach loops in PHP include accidentally overwriting variables from outer loops, leading to unexpected behavior, and inefficient code execution due to unnecessary looping. To avoid these issues, it is important to use unique variable names for iteration in each loop and consider alternative approaches, such as using array functions or restructuring the data to reduce nested loops.

// Example of avoiding pitfalls with nested foreach loops
$outerArray = [1, 2, 3];
$innerArray = ['a', 'b', 'c'];

foreach ($outerArray as $outerValue) {
    foreach ($innerArray as $innerValue) {
        echo $outerValue . ' - ' . $innerValue . PHP_EOL;
    }
}