我想在一个输出 csv 中写入多个数组,其中每个数组代表一列。如果我使用两个循环,输出低于第一个循环的输出..
我的代码是:
$fp = fopen('output.csv','w');
foreach ($ordernumber as $line) {
fputcsv($fp,explode(',',$line));
}
foreach ($currency as $line) {
fputcsv($fp,explode(',',$line));
}
fclose($fp);
这里是输出:
item_sku
1126204
1126220
1126230
1126250
1126252
1126256
1126257
1126258
1126260
1126261
1126116.54
currency
EUR
EUR
EUR
EUR
EUR
EUR
EUR
EUR
EUR
EUR
EUR
EUR
所以,但我需要并排输出 - 就像常规的 csv 文件一样。
有人可以帮我开始吗?
最佳答案
假设两个数组中的行彼此匹配,即第 1 行 = 第 1 行,您必须在一个循环中执行此操作。一种方法是使用正在处理的数组的索引来访问另一个数组。
$fp = fopen('output.csv','w');
// add col headings
fputcsv($fp,[ 'item_sku','currency' ]);
foreach ($ordernumber as $idx => $line) {
fputcsv($fp,[ $line,$currency[$idx] ]);
}
But I would start by looking at how you managed to generate 2 arrays that contain related data but are not the same array and fix that
关于php - 将多个数组作为列保存到 csv 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51728243/