How can PHP values in an array be stored within a foreach loop?
When working with PHP arrays in a foreach loop, you can access and store values from the array using a variable assigned within the loop. To store these values, you can create a new array and push the values into it during each iteration of the loop. This allows you to save and manipulate the values for later use outside of the loop.
// Sample array
$originalArray = [1, 2, 3, 4, 5];
// New array to store values
$newArray = [];
// Loop through the original array and store values in the new array
foreach ($originalArray as $value) {
$newValue = $value * 2; // Manipulate the value if needed
$newArray[] = $newValue; // Store the value in the new array
}
// Output the new array
print_r($newArray);