Retrieve columns data for each line csv - php

I want to retrieve for each line all data for a specific column of a csv file.
File structure
Swimlane,Column,ID,Title
For example retrieve all data for the column "Title".
Code:
$row = 1;
if (($handle = fopen("Business Backlog API Publication.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Column title
$num = $data[4];
echo "<p> $num : $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Thank you so much

If file is not huge, you can use file() function:
$lines = file("Business Backlog API Publication.csv");
$row = 0;
foreach($lines as $line)
{
$line = explode(",", $line);
$title = $line[3];
echo "<p> $row : $title: <br /></p>\n";
$row++;
}
Note that title is at index 3, not 4

Related

How can I analyze the content of the cell in excel using php

I want to analyze the data inside the cell. More specifically, divide it into house number street city.
To get the data from a .csv file you can use this 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);
}
?>
Also you can have a look to the documentation here.
Then to get a particular parts of address, you can use this:
string substr ( string $string , int $start [, int $length ] )
Here you can get more information.

Open csv file improper encoding

When I open the csv file
$str = file_get_contents($filename);
$str = mb_convert_encoding($str,"utf-8","sjis");
var_dump($str);
It shows like this
\0000\000.\0000\0000\0008\0000\0005\0007\000,\0000\000.\0000\0001\0000 ....
At first I think , this is the problem of encoding, so I added mb_convert_encoding but in vain.
What cause this improper encoding?
Better you can do like this:
$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);
}
Code copied from :http://php.net/manual/en/function.fgetcsv.php
alternatively : http://php.net/manual/en/function.str-getcsv.php

PHP open csv and save data to new csv

I need to replace a lot of data in csv and after changing all things i want it saved to a new csv file.
i can read the csv with the code from php.net but i can't get it to work saving the new file. i have this
$row = 0;
if (($handle = fopen("original.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 20000, ";")) !== FALSE) {
$num = count($data);
$cat_alt = ['cold', 'hot', ... and so on...];
$cat_neu = ['kalt', 'heiß', ...und so weiter...];
echo "<p> $num Felder in Zeile $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
$output = str_replace($cat_alt, $cat_neu, $data[$c] . "<br />\n");
echo $output;
}
}
}
$fp = fopen('changed.csv', 'w');
foreach ($row as $rows) {
fputcsv($fp, $rows);
}fclose($fp);
You have two options:
Use n array
$rows = []
...
$rows[] = str_replace($cat_alt, $cat_neu, $data[$c] . "\n");
...
fputcsv($fp, $rows);
Open the output file beforehand and write to it as you process the data
$fp = fopen('changed.csv', 'w');
if (($handle = fopen("original.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 20000, ";")) !== FALSE) {
$num = count($data);
$cat_alt = ['cold', 'hot', ... and so on...];
$cat_neu = ['kalt', 'heiß', ...und so weiter...];
echo "<p> $num Felder in Zeile $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
fputs($fp, str_replace($cat_alt, $cat_neu, $data[$c] . "<br />\n"));
}
}
}
fclose($fp);

How to transform while loop into foreach loop in the context of using fopen and fgetcsv functions?

This is the standard code on php.net:
<?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);
}
?>
Let's assume that test.csv has 3 rows with:
cell11,cell12,cell13
cell21,cell22,cell23
cell31,cell32,cell33
Is it possible to use a foreach loop instead of the while used here and still get the values inside test.csv, for further use?
The code above gives this result:
3 fields in line 1:
cell11
cell12
cell13
3 fields in line 2:
cell21
cell22
cell23
3 fields in line 3:
cell31
cell32
cell33
This is what I have tried:
<?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";
// }
// }
foreach(($data = fgetcsv($handle, 1000, ",")) as $field){
$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);
}
?>
And the result:
3 fields in line 1:
cell11
cell12
cell13
3 fields in line 2:
cell11
cell12
cell13
3 fields in line 3:
cell11
cell12
cell13
This:
foreach(($data = fgetcsv($handle, 1000, ",")) as $field){ ...
Passes the data from fgetcsv to the new variable $data which doesn't make sense in this case. You can just write this:
foreach (fgetcsv($handle, 1000, ",") as $field) { …
But to explain why foreach doesn't work:
As fget (and thus fgetcsv) is a function it does not work as an array, it just returns one, namely the next line in the file.
The foreach example above just gets the first line from that function and iterates over it. This means you iterate over each column of the first line.

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