我正在尝试将此数组(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
由于
答案 0 :(得分:0)
如果我使用你在问题中给出的第一个数组,我会得到一个合适的结果。你应该考虑三件事:
array_values()
fputcsv()
if()
的正确括号以避免混淆我猜你的问题是由函数的错误返回值引起的。 csv导出似乎工作正常。看别处。