0
  • continue on PHP duplicate staffID

  • code
    $data[0] = 0001,Ali,N,OK
    $data[1] = 0002,Abu,N,OK
    $data[2] = 0003,Ahmad,N,OK
    $data[3] = 0004,Siti,Y,Not OK. Only one manager allowed!
    $data[4] = 0005,Raju,Y, Not OK. Only one manager allowed!

    I write it as following:

    for($i = 0; $i < 5; $i++)
    {
        $data[i] = $staffID[$i].','.$staffname[$i].','.$ismanager[$i].','.$remark[$i];
    }
    
  • Next I go to write csv file.

    $file_format = "staffreport.csv";
    $file = fopen($file_format,"w");
    foreach($data as $line)
    {
        $replace = str_replace(",","|", $line);
        fputcsv($file, array($replace));
        echo $replace.'<br />';
    }
    fclose($file);
    
  • output (echo $replace)
    0001|Ali|N|OK
    0002|Abu|N|OK
    0003|Ahmad|N|OK
    0004|Raju|Y|Only one manager allowed!
    0005|Siti|Y|Only one manager allowed!

  • In CSV file (staffreport.csv)
    0001|Ali|N|OK
    0002|Abu|N|OK
    0003|Ahmad|N|OK
    "0004|Siti|Y|Only one manager allowed!"
    "0005|Raju|Y|Only one manager allowed!"

  • Why my csv file have double quote("")? How do I solve it?

1 Answer 1

2

change this bit like so.

for($i = 0; $i < 5; $i++)
{
    $data[i] = array($staffID[$i],$staffname[$i],$ismanager[$i],$remark[$i]);
}

fputcsv wants an array of columns it will then insert the separators and quote marks as needed.

fputcsv usually uses commas for separators, if you want pipes: do this:

 fputcsv($file, $line,'|');

why the quotes? probably because of the spaces.

if you must avoid the quotes,

 fputcsv($file, $line,'|','');

hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.