如何从多维数组创建csv文件

  • 本文关键字:创建 csv 文件 数组 php
  • 更新时间 :
  • 英文 :


我正在从数组创建一个csv文件,但问题是数据有不同的日期,我想用日期来分隔,请帮助我解决这个问题。

我已经创建了一个csv文件,但它只有一行,我想在两个数组之间添加分隔符。

这是阵列响应

Array
(
[0] => 2020-04-16 13:18:05
[1] => stdClass Object
(
[question] => What is your name?
[response] => khushwinder
)
[2] => stdClass Object
(
[question] => What is your age?
[response] => 130
)
[3] => stdClass Object
(
[question] => Please tell me your birth year?
[response] => 01-05-1986
)
[4] => stdClass Object
(
[question] => Are you married?
[response] => yes
)
[5] => stdClass Object
(
[question] => How many children do you have
[response] => 1
)
)
Array
(
[0] => 2020-04-16 13:35:34
[1] => stdClass Object
(
[question] => Hi my name is Matt and im here to help...
[response] => gi
)
[2] => stdClass Object
(
[question] => What is your age?
[response] => happy
)
[3] => stdClass Object
(
[question] => Please tell me your birth year?
[response] => 09-02-1983
)
)
Array
(
[0] => 2020-04-17 08:54:20
[1] => stdClass Object
(
[question] => What is your name?
[response] => jack
)
[2] => stdClass Object
(
[question] => What is your age?
[response] => 30
)
[3] => stdClass Object
(
[question] => Please tell me your birth year?
[response] => 01-01-98
)
[4] => stdClass Object
(
[question] => Are you married?
[response] => yes
)
[5] => stdClass Object
(
[question] => How many children do you have
[response] => 2
)
[6] => stdClass Object
(
[question] => Do you like tea ?
[response] => yes
)
[7] => stdClass Object
(
[question] => Please tell me something about yourself?
[response] => nothing 
)
)

这是我正在处理的代码。

public function download_csv()
{
$filename = 'chatbot_questions_' . date('His') . '.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
$result = $this->script_model->get_chat_by_date($_POST['startdate'], $_POST['enddate'], $this->id);
if (!empty($result)) {
$response = [];
foreach ($result as $key => $res) {
array_push($response, array('created_at' => $res->created_at, 'response' => json_decode($res->response)));
}
foreach ($response as $key => $file) {
$final_data = [];
array_walk_recursive($file, function ($items) use (&$final_data) {
$final_data[] = $items;
});
fputcsv($file, $final_data);
}
}
}

感谢任何解决方案!

您的数组格式不正确:

<?php
function qa($q, $a){
$o = new StdClass; $o->question = $q; $o->answer = $a;
return $o;
}
$yourArrayLooksLike = [
['2020-04-16 13:18:05', qa('What is your name?', 'khushwinder'), qa('What is your age?', '130'), qa('Please tell me your birth year?', '01-05-1986'),
qa('Are you married?', 'yes'), qa('How many children do you have', '1')],
['2020-04-16 13:35:34', qa('Hi my name is Matt and im here to help...', 'gi'), qa('What is your age?', 'happy'),
qa('Please tell me your birth year?', '09-02-1983')],
['2020-04-17 08:54:20', qa('What is your name?', 'jack'), qa('What is your age?', '30'), qa('Please tell me your birth year?', '01-01-98'),
qa('Are you married?', 'yes'), qa('How many children do you have', '2'), qa('Do you like tea ?', 'yes'),
qa('Please tell me something about yourself?', 'nothing')]
];
// print_r($yourArrayLooksLike);
function fixFormat($badFormat){
$fix = [];
foreach($badFormat as $a){
$d = $a[0]; $n = array_slice($a, 1);
foreach($n as $o){
$r = [$d];
foreach($o as $v){
$r[] = $v;
}
$fix[] = $r;
}
}
return $fix;
}
$arrayShouldBe = fixFormat($yourArrayLooksLike);
// print_r($arrayShouldBe); 
$file = 'yourCSV.csv'; $fp = fopen($file, 'w');
foreach($arrayShouldBe as $a){
fputcsv($fp, $a);
}
fclose($fp);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($file));
readfile($file); die;
?>

最新更新