将MySQL查询保存到两个数组

I'm sure this is simple for someone else, but it escapes me. I have a function that generates a .csv file based on a query input from an internal website. The problem is, for speed purposes, I want to run 1 query to save to two different arrays. One of which I can pass to a function, the other to use for printing a table. I've tried to pass the same $result var to the function. It seems to strip the data once sent through function? I need some help.

code for function:

function save_to_csv($result1, $filename, $attachment = false, $headers = true) {
    if($attachment) {
        // send response headers to the browser
        header( 'Content-Type: text/csv' );
        header( 'Content-Disposition: attachment;filename='.$filename);
        $fp = fopen('php://output', 'w');
    } else {
        $fp = fopen($filename, 'w');
    }

    $result1 = mysql_query($query1) or die( mysql_error() );

    if($headers) {
        // output header row (if at least one row exists)
        $row = mysql_fetch_assoc($result1);
        if($row) {
            fputcsv($fp, array_keys($row));
            // reset pointer back to beginning
            mysql_data_seek($result1, 0);
         }
    }

     while($row = mysql_fetch_assoc($result1)) {
         fputcsv($fp, $row);
    }

    fclose($fp);
}

I've tried setting second array like so

    $csv_result = array(); 
    also tried
    $csv_result = $result = mysql_query($query);

I'm assuming it's something here, but I just cant see it.

There's nothing in this code that demonstrates why you need two separate arrays. After the line where you set $result1, you can simply do the following for the exact same effect:

$row = mysql_fetch_assoc($result1);
if ($row) {
    if ($headers) {
        fputcsv($fp, array_keys($row));
    }
    fputcsv($fp, $row);
}

The variable $row hasn't been modified, and is still equal to the data retrieved from $query1. There's really no need to make a duplicate array unless one of them is going to be modified. However, if you want to make a copy of the data at any point, you can just use:

$new_copy = $row;