How can you optimize the code snippet provided to ensure that each ID has the correct corresponding result value in PHP?

The issue in the code snippet provided is that the result values are being overwritten for each ID in the loop, leading to incorrect corresponding values. To solve this issue, we can use an associative array where the ID is the key and the result is the value. This way, each ID will have its own corresponding result value stored in the array.

// Sample data
$ids = [1, 2, 3];
$results = ["result1", "result2", "result3"];

// Create an associative array to store ID and corresponding result
$idResults = [];

// Loop through the IDs and results to populate the associative array
for ($i = 0; $i < count($ids); $i++) {
    $idResults[$ids[$i]] = $results[$i];
}

// Output the associative array
print_r($idResults);