How can loops be used effectively to manipulate arrays in PHP?
Loops can be used effectively to manipulate arrays in PHP by iterating through each element of the array and performing the desired operation. This can include adding, removing, updating, or filtering elements based on certain conditions. By using loops, you can efficiently process large arrays without having to manually handle each element individually.
// Example: Multiply each element of an array by 2
$array = [1, 2, 3, 4, 5];
foreach ($array as $key => $value) {
$array[$key] = $value * 2;
}
print_r($array);