What are some strategies for accessing values outside of a foreach loop in PHP?

When working with a foreach loop in PHP, you may encounter a situation where you need to access values outside of the loop. One common strategy is to store the values in an array or variable as you iterate through the loop, so that you can access them later on. Another approach is to use a flag variable to indicate when a specific value has been found within the loop. You can also consider using a different type of loop, such as a for loop, if you need more control over the iteration process.

// Storing values in an array for later access
$values = [];
foreach ($array as $item) {
    $values[] = $item;
}

// Accessing values outside of the loop
foreach ($values as $value) {
    // Do something with $value
}