How to use fgetcsv to get specific columns from a CSV? - php

I have an interesting piece of work to complete
I have a lot of spreadsheets which I have converted to CSV's - these spreadsheets started out as templates with a set number of columns.
Unfortunately over time people have added columns and removed them.
Is there a way I can adapt the following code to select certain column names from each CSV
foreach($fileinfos as $pathname => $fileinfo) {
if (!$fileinfo->isFile()) continue;
if(substr($pathname,-3) == 'csv'){
$file = fopen($pathname, "r");
while ($line = fgetcsv($file,0,'^','ยฌ')){
echo substr($pathname,56) .' '.$numcols.'<br>';
}
fclose($file);
}
}
UPDATE:
Here is my array of accepted columns
Array
(
[0] => Date Raised
[1] => QA phase
[2] => Item ID
[3] => Screen Number
[4] => Reason
[5] => Matches originals?
[6] => Issue / Comments
[7] => Raise with Client?
[8] => Raised By
[9] => Status
[10] => TP/AS Status Initials
[11] => TP/AS Status date
[12] => TP/AS Status Comment
[13] => Retested by
[14] => Retested date
[15] => Retested Comment
)
here is my array of avalible
Array
(
[0] => Date Raised
[1] => QA phase
[2] => Item ID
[3] => Screen Number
[4] => exam
[5] => Reason
[6] => Matches originals?
[7] => Issue / Comments
[8] => Raise with Client?
[9] => Raised By
[10] => Status
[11] => TP/AS Status Initials
[12] => TP/AS Status date
[13] => TP/AS Status Comment
[14] => dd Comments
[15] => PM Comments
[16] => Retested by
[17] => Retested date
[18] => Retested Comment
)
Array combine dosnt work.

$file = fopen($pathname, 'r');
$headers = fgetcsv($file, 0, '^', 'ยฌ');
while ($line = fgetcsv($file, 0, '^', 'ยฌ')) {
$line = array_combine($headers, $line);
var_dump($line);
// access specific fields using $line['columnName']
}

I would try something like this:
$csv = array();
$columnnames = fgetcsv($fp, 1024);
while (true == ($columns = fgetcsv($fp, 1024))) {
$row = array_combine($columnnames, $columns);
$csv[] = $row;
}
On a CSV file like this
id,name,email
1,benjamin,benjamin#example.com
2,rob,rob#example.com
the $csv variable should contain something like this:
1 => array(
'id' => 1,
'name' => 'benjamin',
'email' => 'benjamin#example.com'
)
...

Related

Issue in parsing csv data php | regex replace commas inside of quotes

Anyone share code in php to parse csv data . While am parsing data i got values inside double quotes as separate array so my logic will change . I need to store this data to mysql DB. My sample csv file is below
com,24,2.1.0.5,en,mido,2020-11-01T03:29:32Z,1604201372915,2020-11-01T03:29:32Z,1604201372915,5,,Nice๐Ÿ‘๐Ÿ‘,2020-11-01T06:34:23Z,1604212463397,"Hi Raju, Thank you so much",497230
com,24,2.1.0.5,en,athene_f,2020-11-01T04:19:52Z,1604204392095,2020-11-01T04:19:52Z,1604204392095,5,,So so,2020-11-01T06:33:58Z,1604212438170,"Hi dev, Thanks",497230
code
$csv_file = 'csv/test.csv';
$file = fopen($csv_file, "r");
fgetcsv($file);
$count=0;
while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$getData = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $getData);
$getData = implode(",", $getData);
$getData = '\''.substr($getData , 1, -1).'\'';
$getData =explode(",", $getData);
$exp_array = explode(",", $getData);
$package_name = trim($getData[0]);
$app_version_code = trim($getData[1]);
$app_version_name = trim($getData[2]);
$reviewer_language = trim($getData[3]);
ob_get_clean();
}
expected output
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => mido
[5] => 2020-11-01T03:29:32Z
[6] => 1604201372915
[7] => 2020-11-01T03:29:32Z
[8] => 1604201372915
[9] => 5
[10] =>
[11] => Nice๐Ÿ‘๐Ÿ‘
[12] => 2020-11-01T06:34:23Z
[13] => 1604212463397
[14] => Hi Raju, Thank you so much
[15] => 497230
)
I just ran this and got the output you indicated you were expecting:
<?php
$file = fopen('input.txt', "r");
while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
print_r($data);
}
Here's input.txt:
com,24,2.1.0.5,en,mido,2020-11-01T03:29:32Z,1604201372915,2020-11-01T03:29:32Z,1604201372915,5,,Nice๐Ÿ‘๐Ÿ‘,2020-11-01T06:34:23Z,1604212463397,"Hi Raju, Thank you so much",497230
com,24,2.1.0.5,en,athene_f,2020-11-01T04:19:52Z,1604204392095,2020-11-01T04:19:52Z,1604204392095,5,,So so,2020-11-01T06:33:58Z,1604212438170,"Hi dev, Thanks",497230
And here's the output:
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => mido
[5] => 2020-11-01T03:29:32Z
[6] => 1604201372915
[7] => 2020-11-01T03:29:32Z
[8] => 1604201372915
[9] => 5
[10] =>
[11] => Nice๐Ÿ‘๐Ÿ‘
[12] => 2020-11-01T06:34:23Z
[13] => 1604212463397
[14] => Hi Raju, Thank you so much
[15] => 497230
)
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => athene_f
[5] => 2020-11-01T04:19:52Z
[6] => 1604204392095
[7] => 2020-11-01T04:19:52Z
[8] => 1604204392095
[9] => 5
[10] =>
[11] => So so
[12] => 2020-11-01T06:33:58Z
[13] => 1604212438170
[14] => Hi dev, Thanks
[15] => 497230
)
If you are not getting the expected output, one other thing to check is the file encoding on your CSV file. It could be that your PHP script is expecting your file to be utf-8 encoded, but your file is using another encoding. There are lots of resources online for detecting file encoding and converting using PHP.
These 2 lines are 100% incorrect...
$getData =explode(",", $getData);
$exp_array = explode(",", $getData);
givn, that $getData contains a string, after explode() it will be an array (reassigned to $getData).
On the next line, this array is tried to be exploded again, but this will fail and give you warnings or errors.
Turn up error reporting level and display all errors on screen during development.

fgetcsv ignores csv from website but not local

i am writing a script to reach out to a website that downloads a csv into cache and then parses the data into an array.
$base_url = "http://www.collincad.org/ccad/propertysearch/download.php?situs_num=1707&situs_street=university&situs_street_suffix=&isd%5B%5D=any&city%5B%5D=any&prop_type%5B%5D=R&prop_type%5B%5D=P&prop_type%5B%5D=MH&active%5B%5D=1&year=2018";
$handle = fopen($base_url, "r");
$flag = true;
while(!feof($handle))
{
$text = fgetcsv($handle, 1024, ",");
if($flag) { $flag = false; continue; }
print $text[1]. " <br>";
}
mysql_close($connect);
When performing the query this way it has the first row and a row of other data and ignores the comma.
$base_url = "export5.csv";
$handle = fopen($base_url, "r");
$flag = true;
while(!feof($handle))
{
$text = fgetcsv($handle, 1024, ",");
if($flag) { $flag = false; continue; }
print $text[1]. " <br>";
}
mysql_close($connect);
but when i manually download the csv file it and read it from the local folder it works as expected... i would prefer not to make this a two step process... im thinking that reading direct from the site with php is the issue, just can figure out how to resolve it.
Thanks
First and foremost when using fopen with a web url, make sure your server is configured to allow it (http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen). Once that is out of the way you should be fine with your code.
The issue though is the CSV format itself.
Looking at the CSV return of that url, its delimiters are tabs, not commas. And I see no enclosures too. So you need to change your fgetcsv to:
$text = fgetcsv($handle, 1024, "\t", '');
And it should begin to return results like this (for each $text):
Array
(
[0] => 15071
[1] => 2018
[2] => P
[3] => Personal
[4] => P-9000-288-0243-1
[5] => N
[6] => ZZZZZZZ BPP # 1707 W UNIVERSITY DR
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] => BPP AT 1707 W UNIVERSITY DR
[13] => KROGER #488
[14] => 1707
[15] => UNIVERSITY DR
[16] => MCKINNEY
[17] => 1707 W University Dr | McKinney, TX 75069
[18] => 844925
[19] => THE KROGER CO
[20] => CMC
[21] => MCKINNEY CITY
[22] => SMC
[23] => MCKINNEY ISD
[24] =>
[25] => Active
[26] => No
[27] =>
)
Also, the first line in the csv file is this:
Line 1:
Array
(
[0] => sep=
[1] =>
)
So you may want to skip the first TWO lines (the second line being the column headers).
Line 2: (column headers)
Array
(
[0] => Property ID
[1] => Year
[2] => Property Type Code
[3] => Property Type Description
[4] => Geographic ID
[5] => Abstract Or Subdivision Code
[6] => Abstract Or Subdivision Description
[7] => Block
[8] => Tract Or Lot
[9] => Mobile Home Park Code
[10] => Mobile Home Park Description
[11] => Mobile Home Park Space
[12] => Legal Description
[13] => Doing Business As
[14] => Street Number
[15] => Street Name
[16] => City
[17] => Complete Address
[18] => Owner ID
[19] => Owner Name
[20] => Taxing City Code
[21] => Taxing City Name
[22] => Taxing School District Code
[23] => Taxing School District Name
[24] => Market Value
[25] => Property Status
[26] => Certified Data
[27] =>
)

csv column data value that breaks PHP code

I have a CSV file that has 2 rows like that :
11,"HUFFY SPORTS DELAWARE","INC.","HUNTINGTON NATIONAL BANK","DEPT. L-2493","COLUMBUS","OH","","","43260-2493","/\ /\ /\/"
12,"OXFORD GOLF","12564 COLLECTIONS CTR DR","","CHICAGO","IL","","",60693,"","912-526-1100",""
I'm reading that file in PHP and populating an array with each data column using the following code :
<?php
$values = array();
$csvFileName = "test.csv";
if (($handle = fopen($csvFileName, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(count($data) == 1){
$f = fopen('php://temp', 'w');
fwrite($f, $data[0]);
rewind($f);
$data = fgetcsv($f, 1000, ",");
fclose($f);
}
for($i=0; $i<sizeof($data); $i++){
$values[$i] = $data[$i];
}
echo "<pre>";
print_r($values);
echo "</pre>";
}
}
?>
so I should get a result like that :
Array
(
[0] => 11
[1] => HUFFY SPORTS DELAWARE
[2] => INC.
[3] => HUNTINGTON NATIONAL BANK
[4] => DEPT. L-2493
[5] => COLUMBUS
[6] => OH
[7] =>
[8] =>
[9] => 43260-2493
[10] => /\ /\ /\
)
Array
(
[0] => 12
[1] => OXFORD GOLF
[2] => 12564 COLLECTIONS CTR DR
[3] =>
[4] => CHICAGO
[5] => IL
[6] =>
[7] =>
[8] => 60693
[9] =>
[10] => 912-526-1100
[11] =>
)
however this string "/\ /\ /\" is breaking my code (sounds like the last double quote is ignored) and I get the first line a column data like the following :
Array
(
[0] => 11,"HUFFY SPORTS DELAWARE","INC.","HUNTINGTON NATIONAL BANK","DEPT. L-2493","COLUMBUS","OH","","","43260-2493","/\ /\ /\""
12
[1] => OXFORD GOLF""
[2] => 12564 COLLECTIONS CTR DR""
[3] => "
[4] => CHICAGO""
[5] => IL""
[6] => "
[7] => "
[8] => 60693
[9] => "
[10] => 912-526-1100""
[11] => ""
)
Is there a way to fix this please?
Set the $escape parameter of the fgetcsv() function to an empty string unless you are actually escaping " with a backslash:
fgetcsv($handle, 1000, ",", '"', '')

Export data to CSV file using PHP - output is empty

I thought it will be easy task, but simple I'm not able to output data from an array to CSV file (saved on a server).
Code for that part looks like:
$fp = fopen('missing-skus.csv', 'w');
foreach ($missing_array as $lines) {
fputcsv($fp, $lines);
}
fclose($fp);
$missing_array looks like:
Array
(
[0] => 5804
[1] => 5803
[2] => 5802
[3] => 5801
[4] => 5800
[5] => 5799
[6] => 5798
[7] => 5797
[8] => 5796
[9] => 5795
[10] => 5794
[11] => 5793
[12] => 5792
[13] => 5791
[14] => 5790
[15] => 5789
[16] => 5788
[17] => 5787
[18] => 5786
[19] => 5785
[20] => 5784
[21] => 5783
[22] => 5782
[23] => 5781
[24] => 5780
[25] => 5779
)
No matter what, file is always blank. Any clue what I have missed?
The second argument to fputcsv is an array of fields. You are passing a string or integer.
If you want one line with the array values as fields, in CSV fashion, then just:
$fp = fopen('missing-skus.csv', 'w');
fputcsv($fp, $missing_array);
If you just want each array value on one line then no need for fputcsv:
file_put_contents('missing-skus.csv', implode("\n", $missing_array));

PHP Array Contain string

i create a method that will read a file with the application/octet type and here are some of the code.
Raw data :
GTHHS;MEKID Interface;5496;2012-07-20;
NM1;081;IN1;980898989;2001-01-15;Mr;Gaf;Uhkil;Uhkil,Gaf;PRI;Gaf
$contents = file_get_contents($tmp_filename);
$stringContents = explode(";", $contents);
Now it gives me this output :
Array
(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
NM1
[4] => 081
[5] => IN1
[6] => 980898989
[7] => 2001-01-15
[8] => Mr
[9] => Gaf
[10] => Uhkil
[11] => Uhkil,Gaf
[12] => PRI
[13] => Gaf
PR1
[14] => 081
[15] => IN1
[16] => 20730089
[17] => 7 The Schooner
[18] => Auhaas
[19] => Huuula Ave
[20] =>
[21] => Kishma
PR2
[22] => 081
[23] => IN1
[24] => 232323233
[25] => 400006
[26] => HGD
[27] => M
[28] => M
[29] => 2007-10-16
[30] => 1976-03-31
);
How can i make the NM1, PR1 as the head of array like this :
Array (
[NM1] = array(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
)
);
I am planning also to make the inner array [0]-[3] as json.
If you explode the contents by \n you have each line starting with that identifier. If you then just explode by ; in that line and add it as a sub array, you got it like you want.
This actually looks like a plain old CSV file with your ifentifier in line one. If so, try something like this:
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, ";", "\"", "\n")) !== FALSE)
{
$key = array_shift($row);
$data[$key] = $row;
}
fclose($handle);
}
echo json_encode($data);
http://php.net/manual/en/function.str-getcsv.php

Categories