What best practices should developers follow when iterating through query results in PHP to avoid overwriting array contents?

When iterating through query results in PHP, developers should avoid overwriting array contents by creating a new array to store the results. This ensures that the original array remains intact and can be accessed later if needed. By using a separate array for storing query results, developers can prevent unintentional data loss or corruption.

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

// Iterate through the query results and store them in the new array
while ($row = $result->fetch_assoc()) {
    $queryResults[] = $row;
}

// Use the $queryResults array for further processing without affecting the original query results