我正在使用PHP将数组写入CSV,但需要一些帮助格式化数据。目前它是:
Array
(
[YYYY-MM-DD] => Array
(
[1] => INT
[2] => INT
[3] => INT
)
[YYYY-MM-DD] => Array
(
[1] => INT
[2] => INT
[3] => INT
)
)
我想写一个像以下的CSV:
YYYY-MM-DD, INT, INT, INT
YYYY-MM-DD, INT, INT, INT
假设你知道如何将CSV写入文件,因为你说你已经这样做了,这里是一种在写入之前将你的键合并到你的数组中的方法。
foreach ($array as $date => $ints) {
fputcsv($file, array_merge([$date], $ints));
}
如果你只想要一个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
示威在这里:https://3v4l.org/mBcWi