我对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文件。