我使用PHP将数组写入CSV但需要一些帮助格式化数据。目前它是:
"<1>"
我想写一个像:
这样的CSVArray
(
[YYYY-MM-DD] => Array
(
[1] => INT
[2] => INT
[3] => INT
)
[YYYY-MM-DD] => Array
(
[1] => INT
[2] => INT
[3] => INT
)
)
答案 0 :(得分:0)
如果你只想要一个PHP输出(没有真正理解你的CSV问题),你可以这样做:
$arr1 = array("2017-02-12" => array(1 => "INT1", 2 => "INT2", 3 => "INT3"),
"2017-04-30" => array(1 => "INT1", 2 => "INT2", 3 => "INT3"),
"2017-11-18" => array(1 => "INT1", 2 => "INT2", 3 => "INT3"));
foreach($arr1 as $key => $value){
$ints = "";
foreach($value as $key2 => $value2){
$ints = $ints.", ".$value2;
}
echo $key.$ints."\n";
}
// Output :
2017-02-12, INT1, INT2, INT3
2017-04-30, INT1, INT2, INT3
2017-11-18, INT1, INT2, INT3
答案 1 :(得分:0)
假设您知道如何将CSV写入文件,因为您说过您已经这样做了,这是在写入之前将键合并到值数组中的一种方法。
foreach ($array as $date => $ints) {
fputcsv($file, array_merge([$date], $ints));
}