php - 无法将数组保存到 csv 文件

标签 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" } } 

一切正常,两个数组的结构相同,我不知道该怎么做。

完整的 Controller 代码: https://justpaste.it/xt7u

谢谢

最佳答案

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

  1. 你的数组中有数组,处理它们
  2. 您不需要在 fputcsv() 中使用 array_values()
  3. 您应该在 if() 中使用适当的括号以避免混淆

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

关于php - 无法将数组保存到 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206506/

相关文章:

javascript - Jquery:Ajax 调用返回 JSON 数据的 php 脚本

javascript - 这段代码如何在数组中搜索多个值?

arrays - 在元胞数组中存储句柄对象时性能下降

php - 制作代码点火器注册表

php - 获取今天的记录实际上是在 PHP 和 MySQL 中获取昨天的记录

php - MySQL错误: Cannot add or update a child row: a foreign key constraint fails

php - 在 PHP 中将 POSIX 模式转换为 PCRE(eregi 到 preg_match)

php - Laravel Eloquent Sum of relationship 的列

时间:2019-03-08 标签:c++tron2darrayrepeating

php - 如何将Word文档中的问题和答案批量上传到MYSQL数据库?