0
比較兩個多維數組並通過匹配值獲取結果。如何爲這些相應的值創建和下載csv文件。我已經嘗試下面csv文件未在for循環中下載
$mycsvfile_result = array();
foreach ($mycsvfile as $arr1)
{
foreach ($mycsvfile_log as $arr2)
{
if($arr2[2] == $arr1[0])
{
$mycsvfile_result[] = array($arr2[2], $arr2[7]);
}
}
}
試圖代碼1 fputcsv
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="demo.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$fp = fopen('php://output', 'w');
$data=$mycsvfile_result;
foreach ($data as $line)
{
fputcsv($fp, $line);
}
fclose($fp);
exit();
試圖代碼2 fputcsv
$csv = "col1,col2 \n";//Column headers
foreach ($mycsvfile_result as $record)
{
$csv.= $record[0].','.$record[1]."\n"; //Append data to csv
}
$csv_handler = fopen ('demo.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
echo 'Data saved to demo.csv';
上面的代碼1周的結果在
id1,email1 id2,email2 id3,email3
這個代碼上述代碼2的結果爲
col1,col2 id1,email1 id2,email2 id3,email3 Data saved to demo.csv
'$ data = array(「{$ arr2 [2]},{$ arr2 [7]}」,「; WAT!你爲什麼不建立一個正常的數組? –
$ data = array(「$ arr2 [2],$ arr2 [7]」,);也嘗試過這一個@MarkBaker – Puvi
每次你發現一些東西是多餘的時候設置標題 –