无法将数组保存到csv文件

时间:2016-08-29 12:35:39

标签: php arrays codeigniter

我正在尝试将此数组(public $ arr = array();从函数返回)保存到csv文件:

array(2) { [0]=> array(30) { ["url"]=> string(13) "http://o2.pl/" ["content_type"]=> NULL ["http_code"]=> int(301) ["header_size"]=> int(101) ["request_size"]=> int(44) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.063) ["namelookup_time"]=> float(0.016) ["connect_time"]=> float(0.031) ["pretransfer_time"]=> float(0.031) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(0) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0.063) ["redirect_time"]=> float(0) ["redirect_url"]=> string(17) "http://www.o2.pl/" ["primary_ip"]=> string(13) "212.77.100.61" ["certinfo"]=> array(0) { } ["primary_port"]=> int(80) ["local_ip"]=> string(12) "192.168.10.9" ["local_port"]=> int(64070) 
    ["title"]=> string(0) "" ["description"]=> string(0) "" ["keywords"]=> string(0) "" ["robots"]=> string(0) "" } [1]=> array(30) { ["url"]=> string(17) "http://www.o2.pl/" ["content_type"]=> string(24) "text/html; charset=utf-8" ["http_code"]=> int(200) ["header_size"]=> int(267) ["request_size"]=> int(48) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.234) ["namelookup_time"]=> float(0.015) ["connect_time"]=> float(0.031) ["pretransfer_time"]=> float(0.031) ["size_upload"]=> float(0) ["size_download"]=> float(229168) ["speed_download"]=> float(979350) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0.047) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(13) "212.77.100.61" ["certinfo"]=> array(0) { } ["primary_port"]=> int(80) ["local_ip"]=> string(12) "192.168.10.9" ["local_port"]=> int(64071)
    ["title"]=> string(20) "o2 - Serce Internetu" ["description"]=> string(91) "o2 to serce internetu, bijące w rytm tego, co najciekawsze, najgorętsze i najważniejsze." ["keywords"]=> string(4) "Brak" ["robots"]=> string(4) "Brak" } } 

使用:

 $filename = "export-to-csv.csv";
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Content-type: text/csv");
                header("Content-Disposition: attachment; filename=\"$filename\"");
                header("Expires: 0");
                $fh = fopen('php://output', 'w');
                $heading = false;
                if (!empty($this->arr))
                    foreach ($this->arr as $row) {
                        if (!$heading) {
                            fputs($fh, array_keys($row));
                            $heading = true;
                        }
                        fputcsv($fh, array_values($row));
                    }
                fclose($fh);

获取空文件,但使用此数组时(由我编写):

array(4) { [0]=> array(4) { ["Key1"]=> string(6) "value1" ["Key2"]=> string(6) "value2" ["Key3"]=> string(6) "value3" ["Key4"]=> string(6) "value4" } [1]=> array(4) { ["Key1"]=> string(6) "value1" ["Key2"]=> string(6) "value2" ["Key3"]=> string(6) "value3" ["Key4"]=> string(6) "value4" } [2]=> array(4) { ["Key1"]=> string(6) "value1" ["Key2"]=> string(6) "value2" ["Key3"]=> string(6) "value3" ["Key4"]=> string(6) "value4" } [3]=> array(4) { ["Key1"]=> string(6) "value1" ["Key2"]=> string(6) "value2" ["Key3"]=> string(6) "value3" ["Key4"]=> string(6) "value4" } } 

一切正常,两个阵列的结构都是一样的,我不知道该怎么做。

完整的控制器代码: https://justpaste.it/xt7u

由于

1 个答案:

答案 0 :(得分:0)

如果我使用你在问题中给出的第一个数组,我会得到一个合适的结果。你应该考虑三件事:

  1. 你的数组中有数组,处理那些
  2. 您无需在array_values()
  3. 中使用fputcsv()
  4. 您应该使用if()的正确括号以避免混淆
  5. 我猜你的问题是由函数的错误返回值引起的。 csv导出似乎工作正常。看别处。

相关问题