How can an array be used effectively to store loop results in PHP?

When we need to store loop results in PHP, we can use an array to effectively store and access the data. By pushing each loop result into the array, we can easily keep track of all the values generated during the loop iteration. This allows us to access and manipulate the loop results later on in our code.

// Initialize an empty array to store loop results
$results = [];

// Loop to generate results
for ($i = 0; $i < 10; $i++) {
    $result = $i * 2; // Example computation
    $results[] = $result; // Store the result in the array
}

// Accessing loop results stored in the array
foreach ($results as $result) {
    echo $result . "\n";
}