简体   繁体   English

所有的csv导出数据都在一列中

[英]all csv export data is in one column

I have code of csv export,which is using by me but the problem is all data is exporting on csv in one column.but i need data must be column separated, 我有我正在使用的csv导出代码,但问题是所有数据都在一列中导出到csv上,但我需要数据必须以列分隔,

 <?php $results= array(0=>array(name => "anil",agency=>"",phone=>"234235",cname=>"Atleta"),1=>array(name => "anil",agency=>"",phone=>"234235",cname=>"Atleta")); $column = array("name","cname","agency","phone"); $writecolumn = array("Nome do Artista","Categoria","Agencia","Telefone"); $csv_export=''; for($i = 0; $i < count($column); $i++) { $csv_export.= $writecolumn[$i]."\\t" ; } $csv_export.= "\\n"; for($j = 0; $j < count($results); $j++) { for($i = 0; $i < count($column); $i++) { $csv_export.= $results[$j][$column[$i]]."\\t"; } $csv_export.= "\\n"; } $categoryname=$results[0]['cname']; $filename = $categoryname."-Category-Artist-Data.csv"; header('Content-type: application/csv'); header('Content-Disposition: attachment; filename='.$filename); echo $csv_export; exit; ?> 

.

Use array_reduce, which reduces array to a single value, by applying function to each element, with initial value passed as third parameter, something like that: 使用array_reduce,通过将函数应用于每个元素并将初始值作为第三个参数传递,将array减少为单个值,如下所示:

$result= array(0=>array(name => "anil",agency=>"",phone=>"234235",cname=>"Atleta"),1=>array(name => "name_anil",agency=>"agency2",phone=>"234235",cname=>"Atleta_2"));
$csv_export = array_reduce($result, function($acc,$row) {
    foreach($row as $key => $value) {
        $acc .= $value;
        $acc .= "\t";
        }
    $acc .= "\n";
    return $acc;
    },
    "Nome do Artista\tCategoria\tAgencia\tTelefone\n")
$categoryname=$results[0]['cname'];
$filename = $categoryname."-Category-Artist-Data.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $csv_export;
exit;

Your code has delimiter (value separator) is 'tab' ('\\t'). 您的代码的定界符(值分隔符)为'tab'('\\ t')。 May be your CSV reading software need some configuration to start using 'tab' ('\\t') as delimiter. 可能是您的CSV阅读软件需要一些配置才能开始使用'tab'('\\ t')作为分隔符。 I updated your code below and use ',' as delimiter. 我在下面更新了您的代码,并使用“,”作为分隔符。 Microsoft Excel use ',' as default delimiter. Microsoft Excel使用','作为默认分隔符。 Please try code below, 请尝试以下代码,

 <?php $results= array(0=>array('name' => "anil",'agency'=>"",'phone' =>"234235",'cname'=>"Atleta"),1=>array('name' => "anil",'agency'=>"",'phone'=>"234235",'cname'=>"Atleta")); $column = array("name","cname","agency","phone"); $writecolumn = array("Nome do Artista","Categoria","Agencia","Telefone"); $csv_export=''; for($i = 0; $i < count($column); $i++) { $csv_export.= $writecolumn[$i]."," ; } $csv_export.= "\\r\\n"; for($j = 0; $j < count($results); $j++) { for($i = 0; $i < count($column); $i++) { $csv_export.= $results[$j][$column[$i]].","; } $csv_export.= "\\r\\n"; } $categoryname=$results[0]['cname']; $filename = $categoryname."-Category-Artist-Data.csv"; header('Content-type: application/csv'); header('Content-Disposition: attachment; filename='.$filename); echo $csv_export; exit; ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM