3
我想保存此陣列(公共$ ARR =陣列();從函數返回)到CSV文件:無法保存數組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
感謝