How can arrays be utilized to store query results and iterate through them multiple times efficiently in PHP?

To store query results efficiently in PHP, you can fetch the results into an array and iterate through them multiple times. This can be achieved by using the fetchAll() method provided by PDO or mysqli to retrieve all the rows from the query result set and store them in an array. By storing the results in an array, you can easily iterate through them multiple times without having to re-fetch the results from the database each time.

// Assuming $pdo is your PDO connection object and $query is your SQL query

// Fetch all results into an array
$results = $pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);

// Iterate through the results multiple times efficiently
foreach ($results as $row) {
    // Process each row as needed
}