在CSV文件的所有字段中插入双引号(PHP5.6.38)

时间:2019-05-24 07:38:45

标签: php json csv fputcsv

我对fputcsv函数有一个问题。当我引入一个外壳参数(双引号)并在Ubuntu中使用php命令执行它时,php脚本不会将此参数插入到csv文件中。

fputcsv($handle,$array,";",'"'); //One try
fputcsv($handle,$array,";","\""); //Second try
fputcsv($handle,$array,";",chr(34)); //Third try
fputcsv($handle,$array,";","""); //Fourth try
fputcsv($handle,$array,";","'"'"); //Fifth try
fputcsv($handle,$array,";",'"','"'); //Sixth try
fputcsv($handle,$array,";",$quote = '"'); //Seventh try

<?php

//An example JSON string.
$jsonString = file_get_contents('archivo.json');
$codificado = utf8_encode($jsonString);

//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($codificado, true);

//Give our CSV file a name.
$csvFileName = 'example.csv';

//Open file pointer.
$fp = fopen($csvFileName, 'w');

$firstLineKeys = false;
//Loop through the associative array.
foreach($jsonDecoded as $row){
        if(empty($firstLineKeys)) {
                $firstLineKeys = array_keys($row);
                fputcsv($fp,$firstLineKeys,';','"');  
                $firstLineKeys = array_flip($firstLineKeys);
        }
        fputcsv($fp,array_merge($firstLineKeys,$row),';','"');  
}

//Finally, close the file pointer.
fclose($fp);
?>

在所有字段中都没有双引号的CSV文件。

0 个答案:

没有答案