What potential pitfalls could arise when using nested loops in PHP?

One potential pitfall when using nested loops in PHP is the risk of creating inefficient code that can lead to performance issues, especially when dealing with large datasets. To avoid this, it's important to carefully optimize the nested loops and ensure that unnecessary iterations are minimized. Additionally, be mindful of the complexity of the algorithm being used within the nested loops to prevent unintended consequences.

// Example of optimizing nested loops by breaking out of inner loop when condition is met

$array1 = [1, 2, 3];
$array2 = [4, 5, 6];

foreach ($array1 as $value1) {
    foreach ($array2 as $value2) {
        if ($value1 + $value2 === 7) {
            echo "Found a pair that sums up to 7: $value1, $value2";
            break; // exit the inner loop once condition is met
        }
    }
}