【问题标题】:Cannot save array to csv file无法将数组保存到 csv 文件
【发布时间】:2017-01-05 11:59:48
【问题描述】:

我正在尝试将这个数组(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

谢谢

【问题讨论】:

    标签: php arrays codeigniter


    【解决方案1】:

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

    1. 您的数组中有数组,请处理这些
    2. 您不需要在fputcsv() 中使用array_values()
    3. 您应该在if() 中使用正确的括号以避免混淆

    我猜你的问题是由于函数的返回值错误造成的。 csv 导出似乎工作正常。看看别处。

    【讨论】:

      猜你喜欢
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 2022-12-23
      • 2014-03-20
      • 1970-01-01
      相关资源
      最近更新 更多