2016-01-14 25 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