首页 > 解决方案 > 如何从多维数组创建 csv 文件?

问题描述

我正在从数组创建一个 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

解决方案


您的数组格式不正确:

<?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;
?>

推荐阅读