-1

嘿伙计们,我有以下几点:

for($i; $i< count($nontimedif); $i++){      
    $data[$i] = array(
      "Driver Chose Non-Compliance:" => $nontimedif[$i] . ", 
      " . $nonfirstname[$i]. ", 
      " . $nonlastname[$i]. ", 
      " . $nonemail[$i]. ", 
      " . .......
    );
}

所以在运行之后我想添加一些新行,因为这比传输到 .csv 文件中。那么我该如何在最后添加呢?

我如何制作 .csv 文件:

function cleanData(&$str)
  {
   // escape tab characters
    $str = preg_replace("/\t/", "\\t", $str);

    // escape new lines
    $str = preg_replace("/\r?\n/", "\\n", $str);

    // convert 't' and 'f' to boolean values
    if($str == 't') $str = 'TRUE';
    if($str == 'f') $str = 'FALSE';

    // force certain number/date formats to be imported as strings
    if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
      $str = "'$str";
    }

    // escape fields that include double quotes
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';

  }

// filename for download
  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

  $flag = false;
  foreach($data as $row) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\r\n";
      $flag = true;
    }
     array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "\r\n";
  }
4

3 回答 3

8
$data[] = array( 'Key:' => 'Whatever you want to add');
于 2013-04-16T20:16:24.723 回答
2

使用 array_push

$additionalRow = array( 'Whatever'.'whatever'.$somethingelse);
array_push($data, $additionalRow);

或者,您可以这样做:

$headers = array_keys($data);
$lastKey = array_pop($data);
$lastKey++;
$data[$lastKey] - $additionalRow;

祝你好运!希望这有帮助!

于 2013-04-16T20:33:34.933 回答
0

对于这种密钥未知的情况,array_push 可能是一个很好的解决方案。你可以检查这个 http://php.net/manual/en/function.array-push.php

于 2013-04-16T20:31:07.750 回答