2016-01-14 29 views
0

我想以CSV格式導出我的表格的內容。 注:我的結果是有一個沒有表頭的內容。 ,所以我想嘗試CSV文件包含麥克特技演員Cascadaeur 先生粉色大佬如何將無標題表的數據導出爲CSV?

這是我的腳本:

<?php 
 

 
$lignes = array(); 
 
$lignes[] = array('header1', 'header2' , 'header3'); 
 
$lignes[] = array('Mike', 'Stuntman', 'Cascadeur'); 
 
$lignes[] = array('Mister', 'Pink', 'Gangster'); 
 

 
array_to_csv_download($lignes,"export.csv"); 
 

 

 
function array_to_csv_download($array, $file, $delimiter=" \t ") { 
 
    
 
    $fichier_csv = fopen($file, 'w+'); 
 
\t // les problèmes d'affichage des caractères internationaux (les accents par exemple) 
 
\t fprintf($fichier_csv, chr(0xEF).chr(0xBB).chr(0xBF)); 
 
\t 
 
\t foreach($array as $ligne){ 
 
\t \t //Formate une ligne en CSV et l'écrit dans le fichier export.csv 
 
\t fputcsv($fichier_csv, $ligne, $delimiter); 
 
\t } 
 

 
\t // fermeture du fichier csv 
 
\t fclose($fichier_csv); 
 

 
\t // how to export a data table without the header of table ? 
 
\t if (!file_exists($file)) { 
 
\t return false; 
 
\t }else { 
 
\t //set appropriate headers 
 
\t //header('Content-Description: File Transfer'); 
 
\t header('Content-Type: application/csv'); 
 
\t header('Content-Disposition: attachment; filename='.basename($file)); 
 
\t header('Expires: 0'); 
 
\t header('Cache-Control: must-revalidate'); 
 
\t header('Pragma: public'); 
 
\t header('Content-Length: ' . filesize($file)); 
 
\t ob_clean(); 
 
\t flush(); 
 

 
\t //read the file from disk and output the content. 
 
\t readfile($file); 
 
\t exit; 
 
\t } 
 
}

回答

0

使用array_shift功能,這將刪除數組的第一個項目:

$lignes = array(); 
$lignes[] = array('header1', 'header2' , 'header3'); 
$lignes[] = array('Mike', 'Stuntman', 'Cascadeur'); 
$lignes[] = array('Mister', 'Pink', 'Gangster'); 

array_shift($lignes); 
print_r($lignes); 
+0

前感謝üu_mulder你的迴應:-) – FG5

0

您可以使用

array_shift($lignes) 

unset($lignes[0]) 

執行

array_to_csv_download($lignes,"export.csv"); 
+0

覺得你對克林ü:-) – FG5

+0

響應歡迎您 – KLin