Removing quotation marks while writing CSV files PHP

824 views Asked by At

My original csv file was like this:

(ID, Country_code, Country, Address, Name, Age, Gender(1 or 0))
001, 1000, "America", "Washington DC", "(Mark", 25, 1
002, 1000, "America", "Washington DC", "Joe", 25, 1
003, 1000, "America, Washington DC", "Chantler)", 25, 1
004, 1001, "Japan", "Tokyo", "(Tanaka", 26, 0
005, 1001, "Japan", "Tokyo", "Satou)", 26, 0

After the coding my csv become like this:

(ID, Country_code, Country, Address, Name, Age, Gender)

001, 1000, America, Washington DC, (Mark,Joe,Chantler), 25, "1
"
002, 1001, Japan, Tokyo, (Tanaka, Satou), 26, "0
"

It automatically includes quotation mark on age and creates the unnecessary line and the quotation mark for the age category.

How could I fix this?

Below is my code (please ignore japanese language comments):

<?php
$nl = "\n"; //New line
$t = "\t"; //Tab space
$line = array(); //Multidimensional array
$result = array(); //Joint multidimensional array
$num = 0; 
$num1 = 0; 
$output = array(); //Array convert from multidimension array to single array
$output1 = array(); //Array merge of temp2 and test array

$ar_1=file("KEN_ALL.CSV", FILE_IGNORE_NEW_LINES);

foreach($ar_1 as $ar1)
{
        $num1++;
        $line[] = explode(',', $array);
}

for($i=0;$i<$num1);$i++)
{
    for($j=0;$j<7;$j++)
    {
    while($j == 0)
    {
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
    while($j == 1)
    {
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
    while($j == 2)
    {
        $line[$i][$j] = str_replace(" ","", $line[$i][$j]); //データ値に空きスペースの削除
        $line[$i][$j] = str_replace('"','', $line[$i][$j]); //データ値に' " 'の削除
        $result[$num][$j] = $line[$i][$j];
        $j++;
    }
    while($j == 3)
    {
        $line[$i][$j] = str_replace(" ","", $line[$i][$j]); //データ値に空きスペースの削除
        $line[$i][$j] = str_replace('"','', $line[$i][$j]); //データ値に' " 'の削除
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
    while($j == 4)
    {
        $line[$i][$j] = str_replace(" ","", $line[$i][$j]); //データ値に空きスペースの削除
        $line[$i][$j] = str_replace('"','', $line[$i][$j]); //データ値に' " 'の削除
        $line[$i+1][$j] = str_replace(" ","", $line[$i+1][$j]); //データ値に空きスペースの削除
        $line[$i+1][$j] = str_replace('"','', $line[$i+1][$j]); //データ値に' " 'の削除
        $result[$num][$j] = $line[$i][$j].$line[$i+1][$j];
        $j++;
    }
    while($j == 5)
    {
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
    while($j == 6)
    {
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
}
$i++;
$num++;
}

function hensyu($v)
{
    return implode(',', $v);
}

$output = array_map("hensyu", $result);


$f_3 = fopen("output.csv") or die ("Unable to open file");
foreach($output as $array)
{
    fputcsv($f_3, explode(',',$array));
}
fclose($f_3);
?>
2

There are 2 answers

5
Vijay Rathore On BEST ANSWER

I tried to run your script . It showed some php errors and warning , which I have fixed.

<?php
$nl = "\n"; //New line
$t = "\t"; //Tab space
$line = array(); //Multidimensional array
$result = array(); //Joint multidimensional array
$num = 0; 
$num1 = 0; 
$output = array(); //Array convert from multidimension array to single array
$output1 = array(); //Array merge of temp2 and test array

$ar_1=file("KEN_ALL.CSV", FILE_IGNORE_NEW_LINES);

foreach($ar_1 as $ar1)
{
    $num1++;
    $line[] = explode(',', $ar1);
}

for($i=0;$i<$num1;$i++)
{
    for($j=0;$j<7;$j++)
    {
    while($j == 0)
    {
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
    while($j == 1)
    {
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
    while($j == 2)
    {
        $line[$i][$j] = str_replace(" ","", $line[$i][$j]); //データ値に空きスペースの削除
        $line[$i][$j] = str_replace('"','', $line[$i][$j]); //データ値に' " 'の削除
        $result[$num][$j] = $line[$i][$j];
        $j++;
    }
    while($j == 3)
    {
        $line[$i][$j] = str_replace(" ","", $line[$i][$j]); //データ値に空きスペースの削除
        $line[$i][$j] = str_replace('"','', $line[$i][$j]); //データ値に' " 'の削除
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
    while($j == 4)
    {
        $line[$i][$j] = str_replace(" ","", $line[$i][$j]); //データ値に空きスペースの削除
        $line[$i][$j] = str_replace('"','', $line[$i][$j]); //データ値に' " 'の削除
        $line[$i+1][$j] = str_replace(" ","", $line[$i+1][$j]); //データ値に空きスペースの削除
        $line[$i+1][$j] = str_replace('"','', $line[$i+1][$j]); //データ値に' " 'の削除
        $result[$num][$j] = $line[$i][$j].$line[$i+1][$j];
        $j++;
    }
    while($j == 5)
    {
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
    while($j == 6)
    {
        $result[$num][$j] = $line[$i][$j]; 
        $j++;
    }
}
$i++;
$num++;
}

function hensyu($v)
{
    return implode(',', $v);
}

$output = array_map("hensyu", $result);


$f_3 = fopen("output.csv",'a+') or die ("Unable to open file");
foreach($output as $array)
{
    fputcsv($f_3, explode(',',$array));
}
fclose($f_3);
?>

And after this , the output it is giving is -

(ID," Country_code",Country,Address,Name(Mark," Age"," Gender(1 or 0))"
2,1000,America,WashingtonDC,JoeChantler),25,1
4,1001,Japan,Tokyo,(TanakaSatou),26,0

If this output is not correct , Let me know the output you expect.

0
user1165759 On

fputcsv function gets an array as input. You should do:

fputcsv($f_3, $array);