我熟悉创建 CSV,然后使用类似的函数向其中添加行fputcsv
,或者手动使用fwrite
. IE:
fwrite($fh, $facility . "\r\n");
fwrite($fh, $s . " - " . $e . "\r\n");
fwrite($fh, "\r\n");
fwrite($fh, "\r\n");
foreach ($first_column as $f){
fwrite($fh, $f . "," . "\r\n");
}
我的$first_column
数组看起来像:
$first_column = array(
'Chocolate Milk',
'White Milk',
'Orange Juice',
'Apple Juice',
);
它会创建一个类似于以下内容的文件:
West Middle School
5/1/2013 - 5/3/2013
Chocolate Milk,
White Milk,
Orange Juice,
Apple Juice,
我想根据日期在下一个字段中添加数据。数据将如下所示:
$second column['05/01/2013'] = array(5,3,2,2);
$third column['05/02/2013'] = array(5,1,3,5);
然后我可以遍历每个日期,并将值与$first_column
映射匹配。
这可以做到吗?或者我是否必须逐行写入数据,例如:
$data_row['chocolate milk'] = array(5,5,3,6);
$data_row['white milk'] = array(3,1,5,0);
我想要的 CSV 是这样的:
West Middle School
5/1/2013 - 5/3/2013
,5/1/2013,5/2/2013
Chocolate Milk,5,5
White Milk,3,1
Orange Juice,2,3
Apple Juice,2,5