What potential pitfalls can arise when using nested foreach loops in PHP?

Potential pitfalls when using nested foreach loops in PHP include issues with variable scope, unintended overwriting of loop variables, and increased complexity leading to difficult debugging. To avoid these problems, consider using different variable names for each nested loop and ensuring that the data structure being iterated over is consistent.

// Example of avoiding pitfalls with nested foreach loops
$outerArray = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

foreach ($outerArray as $innerArray) {
    foreach ($innerArray as $value) {
        echo $value . " ";
    }
    echo "\n";
}