What are some best practices for storing values from a foreach loop in an array for future use in PHP?

When using a foreach loop in PHP to iterate over an array and you want to store the values for future use, you can create an empty array before the loop and then push each value into the array during each iteration. This way, you can access the stored values outside of the loop.

// Initialize an empty array to store values
$storedValues = [];

// Iterate over the array with a foreach loop
foreach ($array as $value) {
    // Store each value in the array
    $storedValues[] = $value;
}

// Now you can access the stored values outside of the loop
print_r($storedValues);