所以我有一个多维数组——$result
看起来像这样:
[3] => stdClass Object
(
[ProfileID] => 7656
[Practice_Name] => Some Name, MD
[some_id] =>
[Telephone] => (816)531-0930
[Email] => some@some.com
[Contact_Name] => Some Name
[Address] => Some Rd, Ste 40
[City] => Some City
[State] => MO
[ZipCode] =>
[Last_Accessed] => 123479
[Some_statis] => Active
)
我正在使用以下代码将所有数组打印到 drupal 中的 CSV:
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0");
header("Content-Disposition: attachment; filename=\"contacts.csv\";" );
header("Content-Transfer-Encoding: binary");
foreach($result as $row) {
$line = implode(",", (array) $row) . PHP_EOL;
print $line;
}
drupal_exit();
现在它将多维数组的所有内容拆分为列。但问题是,如果结果中有一个逗号,那么就会添加一个额外的列。所以“Some Name, MD”会导致 MD 被推送到不同的列。
知道如何避免这种情况吗?我可以删除那里的逗号,但没有做 preg_replace 的运气。