如何在Codeigniter中将数组导出为CSV? [英] How to export array to CSV in Codeigniter?
本文介绍了如何在Codeigniter中将数组导出为CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$Data[] = array('x'=> $x, 'y'=> $y, 'z'=> $z, 'a'=> $a);
我想将此数组导出为CSV。我使用CodeIgniter。
I want to export this array to CSV. I am using CodeIgniter.
推荐答案
您可以尝试此导出数组到CSV的代码。
you can try this code for your export array to 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;
}
}
这篇关于如何在Codeigniter中将数组导出为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文