Hello I have a csv file when I open it in excel it looks like clean with no quotes at the starting or the ending of each line, same thing in wordpad it looks like this :
10038,"Service, Inc.",loten,[email protected],9951,xxx,6321
10041,RoadsideInc.,Kgel,[email protected],1101,xxx,7967
10042,RangLLC,Resgers,[email protected],2073,4611,xxx
10050,5-Dg,5-dg,[email protected],3-4874,2838389,xxx
10053,SandserviceInc.,Service Center Inc.,[email protected],6-5556,861398,xx
10055,CenStammddy ,Mman,[email protected],6-1449,xx,36-3755
10060,"BetsyRES, C",ElHies,[email protected], 8-0306,237602,xx
10061,Hampnemes LLC,[email protected],83-7999,xxx,-74-1043,xx
10066,CarReal LLC,CarPRea LLC,[email protected],8889,456440,xx
when I read that file in PHP via the script below I get the valid result I want wich is like the folowing :
Array
(
[0] => 10038|Service, Inc.|loten|[email protected]|9951|xxx
[1] => 10041|RoadsideInc.|Kgel|[email protected]|1101|xxx
[2] => 10042|RangLLC|Resgers|[email protected]|2073|4611
[3] => 10050|5-Dg|5-dg|[email protected]|3-4874|2838389
[4] => 10053|SandserviceInc.|Service Center Inc.|[email protected]|6-5556|861398
[5] => 10055|CenStammddy |Mman|[email protected]|6-1449|xx
[6] => 10060|BetsyRES, C|ElHies|[email protected]| 8-0306|237602
[7] => 10061|Hampnemes LLC|[email protected]|83-7999|xxx|-74-1043
[8] => 10066|CarReal LLC|CarPRea LLC|[email protected]|8889|456440
)
when I read the file and save the array I get into a new csv file as output using the PHP code down below I get an output csv file that looks ok in excel but opened in wordpad I see many lines that start and end with quotes like the following :
"10038|Service, Inc.|loten|[email protected]|9951|xxx"
10041|RoadsideInc.|Kgel|[email protected]|1101|xxx
10042|RangLLC|Resgers|[email protected]|2073|4611
10050|5-Dg|5-dg|[email protected]|3-4874|2838389
"10053|SandserviceInc.|Service Center Inc.|[email protected]|6-5556|861398"
"10055|CenStammddy |Mman|[email protected]|6-1449|xx"
"10060|BetsyRES, C|ElHies|[email protected]| 8-0306|237602"
"10061|Hampnemes LLC|[email protected]|83-7999|xxx|-74-1043"
"10066|CarReal LLC|CarPRea LLC|[email protected]|8889|456440"
Is there something wrong with the way I'm saving the csv output file please?
<?php
function createCSV($rows){
$csv_filename = "output.csv";
$fp = fopen($csv_filename, "w");
foreach ($rows as $field) {
fputcsv($fp, array($field));
}
fclose($fp);
echo "csv created";
}
$result = array();
$row = 1;
if (($handle = fopen(__DIR__.'/stack.csv', "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data_row = array(
$data[0],
$data[1],
$data[2],
$data[3],
$data[4],
$data[5]
);
$result[] = implode("|", $data_row);
$row++;
}
}
echo "<pre>";
print_r($result);
echo "</pre>";
fclose($handle);
createCSV($result);
the csv input and output files are below :
implode("|", $data_row)to fix all items into one so that's what you get: one value.