如何用php转换CSV文件



只是想知道如何在CSV文件中显示不同的数据,代码现在有了显示数据像这样inexcel/google

但是我想在excel/google文档中像这样显示数据

这是我正在使用的代码是如何修改它给我我想要的结果?

$data2 = [
["number of drinks:" => "1"],
["number of snacks:" => "2 "],
["number of cats" => "3"],
["numbers of dogs:" => "4"],
$fh = fopen('file.csv', 'w');
$headers = [];
$values = [];
foreach ( $data2 as $form_section ) {
foreach ( $form_section as $heading => $value ) {
array_push( $headers, $heading );
array_push( $values, $value );
}
}
fputcsv( $fh, $headers,);
fputcsv( $fh, $values,);
fclose($fh);
];

这将生成传递给fputcsv所需格式的数组。

$data2 = [
["number of drinks:" => "1"],
["number of snacks:" => "2 "],
["number of cats" => "3"],
["numbers of dogs:" => "4"],
];
$tmp = [];
$fh = fopen("file.csv", "w");

foreach($data2 as $row){ 
$header = key($row);
$value = $row[$header];

fputcsv( $fh, [$header, $value]);
}
fclose($fh);

相关内容

  • 没有找到相关文章

最新更新