使用PHP批量插入/导出数组到CSV文件

时间:2016-11-16 07:17:44

标签: php arrays csv bulkinsert

我想将关联数组导出为.csv文件。我在foreach循环中使用fputcsv()一次插入一行。

这是我的代码:

$headers = array("Parent ID", "Company Name", "Created By", "Created Time", "Mandatory", "Valid", "Total Count", "Audit");

    foreach ($cursorCount as $eachCursorCount) {
        foreach ($cursor as $eachCursor) {
            if ($eachCursorCount["createdBy"] == $eachCursor["createdby"]) {
                $insertVal = array(
                    "parentid" => $eachCursor['parentid'],
                    "companyName" => $eachCursor['companyname'],
                    "createdby" => $eachCursor['createdby'],
                    "createdtime" => date('Y-m-d H:i:s', $eachCursor['createdtime']->sec),
                    "mandatory" => $eachCursor['mandatory'],
                    "valid" => $eachCursor['valid'],
                    "totalCount" => $eachCursorCount['totalCount'],
                    "audit" => $eachCursorCount['audit']
                );
                array_push($res_arr_values, array_values($insertVal));
                }
            }
        }
    }
    $fp1 = fopen('export.csv', 'w');
    fputcsv($fp1, $headers);
    foreach ($res_arr_values as $fields) 
    {
        fputcsv($fp1, $fields);
    }

    fclose($fp1);

是否有任何方法可以一次插入多行? (或任何其他时间复杂度较低的方法。)

谢谢。

0 个答案:

没有答案