What are the potential pitfalls of using foreach loops in PHP when iterating through arrays?

Potential pitfalls of using foreach loops in PHP when iterating through arrays include modifying the array within the loop, which can lead to unexpected behavior, and not being able to access the index of the current element easily. To solve these issues, you can use a for loop instead of a foreach loop to have more control over the iteration process.

// Using a for loop to iterate through an array
$array = [1, 2, 3, 4, 5];
$arrayLength = count($array);

for ($i = 0; $i < $arrayLength; $i++) {
    echo "Element at index $i is: " . $array[$i] . PHP_EOL;
}