How can the issue of overwriting array values be resolved in a PHP foreach loop?

When iterating over an array using a foreach loop in PHP, if you modify the array values directly within the loop, you may inadvertently overwrite the original values. To avoid this issue, you can create a copy of the array and modify the copy instead. This way, the original array remains unchanged.

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

// Create a copy of the original array
$copyArray = $originalArray;

// Modify the copy array within the foreach loop
foreach ($copyArray as &$value) {
    $value *= 2;
}

// Output the modified copy array
print_r($copyArray);

// Output the original array to show it remains unchanged
print_r($originalArray);