PHP - parse a csv file and write output to another csv file - php

I am working on Magento products csv file and i have a csv file with 3 columns like
<pre>Name, description, Color</pre>
<pre>t-shirt, tshirt description, "green, blue, yellow"</pre>
Now based on the values of column 3rd column (color) i want to output each row in a separate file say "new_products.csv" in a manner so the resultant file would be like
<p>Name, description, Color</p>
<pre>t-shirt, tshirt description, green</pre>
<pre>t-shirt, tshirt description, blue</pre>
<pre>t-shirt, tshirt description, yellow</pre>
i have started with this code
$row = 1;
if (($handle = fopen("product.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$name = $data[0];
$desc = $data[1];
for ($c=0; $c < $num; $c++) {
$fp = fopen('new_products.csv', 'w');
if($c == 2){ //if we are at color field
$color = explode(',', $data[$c]);
$color_count = count($color);
for($i=0; $i<$color_count; $i++){
fputcsv($fp, array($name,$desc,$color[$i]));
}
}
}
$row++;
}
fclose($handle);
}
However the above code only outputting the last row only.
Thanks

Finally i have been able to find the solution my self. Actually one need to set the file pointer at the end of the file.
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$name = $data[0];
$desc = $data[1];
for ($c=0; $c < $num; $c++) {
if($c == 2){ //if we are at color field
$fp = fopen('file.csv', 'a'); //Open for writing only; place the file pointer at the end of the file.
$color = explode(',', $data[$c]);
$color_count = count($color);
for($i=0; $i<$color_count; $i++){
fputcsv($fp, array($name,$desc,$color[$i]));
}
fclose($fp);
}
}
$row++;
}
fclose($handle);
}

Related

Getting a CSV cell full value in PHP

Am reading a CSV file using PHP, i get all the values for each row but some columns having long data displays something like this 9.21008E+15
I don't know how to get the complete value which is suppose to be this 9210080000000000
if (($handle = fopen("Test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
$tb_row = "";
for ($c=0; $c < $num; $c++) {
$tb_row .= $data[$c].",";
}
$new_row = substr($tb_row, 0, -1);//removing the comma at the end of line...
//explode and assign values then insert
$each_col = explode(',', $new_row);
$Device_state = $each_col[0];
$udid = $each_col[8];
$imsi = $each_col[9];
$imei = $each_col[10];
echo "$imsi || $imei"."<br />";
}
}
Adding a prefix to the cells having this issue solved the issue (')
You need just convert it back into integer value
if (($handle = fopen("Test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
$tb_row = "";
for ($c=0; $c < $num; $c++) {
if (strpos($data[$c], 'E+'))
$data[$c] = intval((float)$data[$c]);
$tb_row .= $data[$c].",";
}
$new_row = substr($tb_row, 0, -1);//removing the comma at the end of line...
//explode and assign values then insert
$each_col = explode(',', $new_row);
$Device_state = $each_col[0];
$udid = $each_col[8];
$imsi = $each_col[9];
$imei = $each_col[10];
echo "$imsi || $imei"."<br />";
}
}

Scan a csv file for certain value with php

I'm just wondering is it possible to have a csv file (with many values) and scan the file for a specific value with php?
Ideally if this is possible, Id also like to get the index of that value in the file (i.e what row number the value falls on).
I'm quiet new to php so I'm not sure where to start.
All help is greatly appreciated.
Sure,
Not tested:
<?php
$search = 'mysearchstring';
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++)
{
if ($data[$c] == $search)
{
echo "Found in row $row in column {$c+1} <br />\n";
}
}
}
fclose($handle);
}
?>

How to store each row of the csv file to array in php

I have data in csv file as follows.
Mr,Vinay,H S,Application Engineer,vinay.hs#springpeople.com,99005809800,Yes
Mr,Mahammad,Hussain,Application Engineer,vinay.hs#springpeople.com,99005809800,Yes
I want to store each row in each array.explain me how to do that
I did not understand each row in each array.
use fgetcsv() function to read the CSV file php.
An example
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
OR
If you want to store them in an array you could use
$data = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = $row;
}
}
Assuming the CSV is a file (if not, make the CSV match the $csv, which is essentially the file broken down in the separate lines), here is a very simple way:
$csv = file('mycsv.csv'); $result = array();
//iterate the lines
foreach ($csv as $line)
// I'd recommend more robust "csv-following" method though (fgetcsv maybe)
$result[] = explode(',',$result);
//show the results
print_r($result);
Though fgetcsv is probably a safer bet (as others have mentioned) (See the docs).
To further extend your comment:
//iterate the lines
foreach ($csv as $line)
// I'd recommend more robust "csv-following" method though (fgetcsv maybe)
$result[] = explode(',',$result);
can become:
//iterate the lines
foreach ($csv as $line)
// combine the current results with the new results
$result = array_marge($result,$line);
or, if order is important:
//iterate the lines
foreach ($csv as $line)
// add each result on to the end of the result array
foreach($line as $val)
// add each value
$result[] = $val;
You can use fgetcsv() for that:
$data = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = $row;
}
}

formatting fgetcsv output

Im trying to figure out how to take the data returned by fgetcsv, and format it into a readable/editable table, and then use fputcsv to save that table
so far i have this
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "\n";
}
}
fclose($handle);
?>
The following code will output the CSV in an html table. To make it editable wrap the echo ..$val.. with tags and add a php form handler that takes the result and reforms a CSV
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
echo("<table>");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo("<tr>\r\n");
foreach ($data as $index=>$val) {
echo("\t<td>$val</td>\r\n");
}
echo("</tr>\r\n");
}
echo("</table>");
fclose($handle);
?>

reading first 5 fields of csv file in php

I am confused on how to read a csv file in php ( only the first 4 fields) and exclude the remaining fileds.
Sno,Name,Ph,Add,PO
1,Bob,0102,Suit22,89
2,Pop,2343,Room2,78
I just want to read first 3 fileds and jump to next data. cannot figure out how to do through fgetcsv
any help?
Thanks,
I added a comment to the sample fgetcsv code for you:
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
// iterate over each column here
for ($c=0; $c < $num; $c++) {
// handle column data here
echo $data[$c] . "<br />\n";
// exit the loop after 3rd column parsed
if ($c == 2) break;
}
++$row;
}
fclose($handle);
}
Here's a way to do it without the fgetcsv function:
$lines = file('data.csv');
$linecount = count($lines);
for ($i = 1; $i < $linecount; $i++){
$fields = explode(',', $lines[$i]);
$sno = $fields[0];
$name = $fields[1];
$ph = $fields[2];
$add = $fields[3];
}

Categories