What are some best practices for optimizing array manipulation in PHP to avoid unexpected results?

When manipulating arrays in PHP, it's important to be mindful of potential side effects that can lead to unexpected results. One common issue is modifying an array while iterating over it, which can cause elements to be skipped or processed incorrectly. To avoid this, it's recommended to create a copy of the array before making any modifications.

// Example of optimizing array manipulation in PHP to avoid unexpected results

// Create a copy of the original array to avoid side effects
$originalArray = [1, 2, 3, 4, 5];
$modifiedArray = $originalArray;

// Modify the copied array without affecting the original
foreach ($modifiedArray as $key => $value) {
    $modifiedArray[$key] = $value * 2;
}

// Output the modified array
print_r($modifiedArray);