How can the foreach loop be used effectively in PHP to manipulate arrays?

The foreach loop in PHP is a powerful tool for iterating over arrays and manipulating their elements. To effectively use the foreach loop to manipulate arrays, you can access and modify array values within the loop using references. This allows you to update array elements directly without needing to know their keys.

// Example of using foreach loop to manipulate array elements
$numbers = [1, 2, 3, 4, 5];

foreach ($numbers as &$value) {
    $value *= 2; // Double each element in the array
}

print_r($numbers); // Output: Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 )