2016-08-12 32 views
0
$Data[] = array('x'=> $x, 'y'=> $y, 'z'=> $z, 'a'=> $a); 

我想將此數組導出爲CSV。我正在使用CodeIgniter。如何在Codeigniter中將數組導出爲CSV?

+1

你可以參考這個鏈接供你參考:http://stackoverflow.com/questions/3933668/convert-array-into-csv –

回答

5

您可以嘗試將此代碼導出到CSV導出數組。

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 

class Import extends CI_Controller { 

     public function __construct() { 
      parent::__construct(); 
     } 
     public function exports_data(){ 
      $data[] = array('x'=> $x, 'y'=> $y, 'z'=> $z, 'a'=> $a); 
      header("Content-type: application/csv"); 
      header("Content-Disposition: attachment; filename=\"test".".csv\""); 
      header("Pragma: no-cache"); 
      header("Expires: 0"); 

      $handle = fopen('php://output', 'w'); 

      foreach ($data as $data) { 
       fputcsv($handle, $data); 
      } 
       fclose($handle); 
      exit; 
     } 
} 

我希望它能幫助你。