简体   繁体   中英

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,

 <?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:

$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'). May be your CSV reading software need some configuration to start using 'tab' ('\\t') as delimiter. I updated your code below and use ',' as delimiter. Microsoft Excel use ',' as default delimiter. 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; ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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