There are a few "it depends" things here. If you are using something like the PDO database extension with a prepared statement, after you execute the statement, you can dump the entire result into one 2-D array:
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
Then you have all the results in that array, which you can loop through and manipulate however you want.