我正在将数据导出到csv,我有多个要添加到csv文件的阵列。我可以将数据插入第一个数组的csv中。
我想要的是把第二个数组数据和第一个数组数据放在一起。
示例csv:
Array1: Array2: Array3:
1 2 3 4 a b c d xd xy cd
5 6 7 8 e f g h dg dy cs代码:
$output = fopen('php://output', 'w+');
foreach ($ofdPerCompany as $row){
$array = array( $row->company_name, $row->countofwaybill);
fputcsv($output, $array); // here you can change delimiter/enclosure
}
// tell the browser it's going to be a csv file
header('Content-Type: application/csv');
// tell the browser we want to save it instead of displaying it
header('Content-Disposition: attachment; filename="file.csv";');
fclose($output);有人能帮我把csv分成不同的列吗?
发布于 2020-07-09 18:00:30
如果您有三个具有不同记录数量的数组,则可以执行以下操作:
$first = [];
$second = [];
$third = [];
// Get maximum size
$maxLength = max(count($first), count($second), count($third));
// Write headers manually
fputcsv($fh, ["First", "Second", "Third"]);
for ($i = 0; $i < $maxLength; $i++) {
// If key does not exist use empty string
$data = [
$first[$i] ?? "",
$second[$i] ?? "",
$third[$i] ?? ""
];
fputcsv($fh, $data);
}https://stackoverflow.com/questions/62812100
复制相似问题