What are some potential pitfalls when restructuring arrays in PHP using foreach loops?

One potential pitfall when restructuring arrays in PHP using foreach loops is that modifying the array directly within the loop can lead to unexpected results or errors due to the array being modified while being iterated over. To avoid this issue, it is recommended to create a new array and populate it with the desired values instead of modifying the original array.

// Original array
$originalArray = [1, 2, 3, 4, 5];

// New array to store restructured values
$newArray = [];

// Restructuring the original array using foreach loop
foreach ($originalArray as $value) {
    // Modify the value as needed
    $newValue = $value * 2;
    
    // Add the modified value to the new array
    $newArray[] = $newValue;
}

// Output the restructured array
print_r($newArray);