What are the potential pitfalls of using nested foreach loops in PHP when working with arrays?

Using nested foreach loops in PHP when working with arrays can lead to performance issues, especially with large arrays. Each nested loop iteration increases the time complexity, potentially resulting in slower execution times. To avoid this, consider using alternative methods such as array functions like array_map or array_walk, or restructuring your array to reduce the need for nested loops.

// Example of restructuring array to avoid nested loops
$array = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

foreach ($array as $subArray) {
    // Process each sub-array here
    foreach ($subArray as $value) {
        // Process each value here
    }
}