簡體   English   中英

fput csv不添加第二行

[英]fput csv not adding second line

有人可以看到這段代碼有什么問題嗎? $ output是一個多維數組,因此我將其展平,然后將鍵和值拆分為單獨的數組。 我已經做了$ header和$ answers的var_dump,但是由於某種原因fputscsv($buffer, $answers)沒有做任何事情。 標題看起來不錯,所以該行周圍的內容正在中斷:/

function buildcsv($output)
{
  $headers = array();
  $answers = array();

  foreach($output as $section) // Flatten sections into single array and remove config rows.
  {
     array_shift($section);
     array_pop($section);
     array_pop($section);

     foreach($section as $question => $answer)
     {
       array_push($headers, $question); // Build array for header row
       array_push($answers, $answer); // Build array for answers row
     }
  }

     array_push($headers, "Read terms and conditions"); // Add terms and consitions column.
     array_push($answers, "Yes");

     $buffer = fopen('php://temp', 'r+');
     fputcsv($buffer, $headers);
     fputcsv($buffer, $answers);
     rewind($buffer);
     $csv = fgets($buffer);
     fclose($buffer);
     return $csv;

 }

不能確定,但​​是最好這樣寫:“ php://temp/file.csv”?

因此,基於這樣的事實,由於某種原因,我似乎只能正確訪問輸出緩沖區,這就是我想到的。 該功能允許另一個代碼塊將表單的響應轉換為CSV格式,然后將其附加到電子郵件中。

// Generate csv
 function buildcsv($output)
 {
     $headers = array();
     $answers = array();

     foreach($output as $section) // Flatten sections into single array and remove config rows.
     {
        array_shift($section);
        array_pop($section);
        array_pop($section);
        foreach($section as $question => $answer)
        {
                array_push($headers, $question); // Build array for header row
                array_push($answers, $answer); // Build array for answers row
        }
     }

     array_push($headers, "Read terms and conditions"); // Add terms and conditions column.
     array_push($answers, "Yes");

     $buffer = fopen('php://output', 'r+');

     ob_start();

     fputcsv($buffer, $headers);
     fputcsv($buffer, $answers);

     rewind($buffer);
     $csv = ob_get_contents();

     ob_end_clean();
     return $csv;

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM