2015-11-11 128 views
0

我目前有一個查詢,返回下面的數組..當我嘗試將數據導出到CSV文件時,我看到數據兩次(列重複,而不是行)..可以有人向我解釋爲什麼? (我想有每個記錄顯示只有一次)PHP導出數組爲CSV

Array 
(
    [0] => Array 
     (
      [0] => 6991 
      [id] => 6991 
      [1] => 8588 
      [work_num] => 8588 
      [2] => Test123 
      [name] => Test123 
      [3] => 1407154 
      [deployed] => 1407154 
      [4] => 2015-10-13 
      [created_date] => 2015-10-13 
     ) 
) 

這是我使用。如果你正在使用MySQL的數據

// create a file pointer connected to the output stream 
$output = fopen('php://output', 'w'); 

// output the column headings 
fputcsv($output, array('id','Work Number','Name','Deployed','Date')); 


$get_data = $CLASSNAME->FuntionName(Parameters); 

foreach($get_data as $data_row) { 
    fputcsv($output, $data_row); 
} 

fclose($output); 
+1

你的數據是作爲一個MySQL查詢結果。郵政編碼獲取數據。 –

回答

2

導出代碼,然後更換mysql_fetch_arraymysql_fetch_row

mysql_fetch_array返回結果集作爲數字和關聯數組。 mysql_fetch_row將僅返回結果集作爲數字數組。

1

無論哪種解決您的輸入數據(使用PDO時,使用PDO :: FETCH_ASSOC或PDO :: FETCH_NUM代替PDO :: FETCH_BOTH,這是默認值)或命名列投入到CSV時:

foreach($get_data as $data_row) { 
    fputcsv($output, array(
     $data_row['id'], 
     $data_row['work_num'], 
     $data_row['name'], 
     $data_row['deployed'], 
     $data_row['created_date'] 
    )); 
} 
+0

我不得不做一個小改動.. $ data_row [$ i] ['id'] –