What are the potential pitfalls of using foreach loops to process large arrays in PHP?

Using foreach loops to process large arrays in PHP can be inefficient because each iteration creates a copy of the array element, which can consume a lot of memory for large arrays. To solve this issue, you can use a for loop instead of a foreach loop to directly access array elements by their index without creating copies.

// Example of using a for loop to process a large array efficiently
$largeArray = range(1, 1000000);

for ($i = 0; $i < count($largeArray); $i++) {
    // Process each element of the array here
    echo $largeArray[$i] . PHP_EOL;
}