when i am trying to upload csv file in php and exporting fields in php using fgetcsv function, it is returned all result in one array instead of comma separated.
I have 2 csv file but in one its working and in another it is showing all values in one array
like this:
Array
(
[0] => Name
[1] => Number
[2] => Tax Center
John12
[3] => 2122561681
[4] => 71 West 19
John13
[5] => 6462102791
[6] => 72 West 19
John14
[7] => 9144190749
[8] => 73 West 19
John15
[9] => 8602867600
[10] => 74 West 19
John16
[11] => 8602435024
[12] => 75 West 19
John17
[13] => 8602429299
[14] => 76 West 19
John18
[15] => 2034838300
[16] => 77 West 19
here is my code to get only first row of csv
if(($handle = fopen($file , "r")) !== FALSE)
{
$fileAdded = true;
$adcon = 0;
while (($data = fgetcsv($handle, 0, ",")) !== FALSE)
{
/*echo '<pre>';
print_r($data);
echo '</pre>';*/
$first = $data;
break;
}
fclose($handle);
}
Related
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.
I am trying to separate and write chunks of n datapoints recursively from the first to n.
e.g. I want the first 20, then the next 20 (starting from the second point), and so on. From 1 to 20 (in a dataset of 40 points).
It seems to work at the beginning then the last chunks are offset by 1 point. I'm sure it has to be the most obvious error but I just can't wrap my head around it!!!
I've tried array_slice() and a for() loop. Both give me the same error.
function array_smas($series, $length){ # Uses array_slice()
for($i = 0; $i < $length; $i++){
echo "i = $i\n";
$offset = $length - $i - 1;
echo "offset = $offset\n";
$series_n = array_slice($series, $offset, $length);
file_put_contents(__DIR__."/file.txt", print_r($series_n, true), FILE_APPEND);
file_put_contents(__DIR__."/file.txt", "\n\n", FILE_APPEND);
}
return 0;
}
function array_smas_forloop($series, $length){ # Uses for-loop
for($i = 0; $i < $length; $i++){
echo "i = $i\n";
$series_n = array();
for($c = 0; $c < $length; $c++){
$series_n[] = $series[$c + $i];
}
file_put_contents(__DIR__."/file.txt", print_r($series_n, true), FILE_APPEND);
file_put_contents(__DIR__."/file.txt", "\n\n", FILE_APPEND);
}
return 0;
}
Using a dataset of 40 points, the first and last arrays and $i outputs should be:
i = 0
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => 12
[12] => 13
[13] => 14
[14] => 15
[15] => 16
[16] => 17
[17] => 18
[18] => 19
[19] => 20
)
...
i = 18
Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
[8] => 28
[9] => 29
[10] => 30
[11] => 31
[12] => 32
[13] => 33
[14] => 34
[15] => 35
[16] => 36
[17] => 37
[18] => 38
[19] => 39
)
i = 19
Array
(
[0] => 21
[1] => 22
[2] => 23
[3] => 24
[4] => 25
[5] => 26
[6] => 27
[7] => 28
[8] => 29
[9] => 30
[10] => 31
[11] => 32
[12] => 33
[13] => 34
[14] => 35
[15] => 36
[16] => 37
[17] => 38
[18] => 39
[19] => 40
)
The problem seems to be that you can't have $i = 19 and the first element as 21. My logic is that if you look at the first loop where $i = 0, the first element is 1 - so 1 difference. BUT when it's 19, you want the first element to be 21 - 2 higher than this number.
If in your array_smas_forloop() you change the limit of the loop to...
for($i = 0; $i <= $length; $i++){
then you should get the desired output.
BUT, there is a lot of array creating and file opening and closing going on. Another alternative is to create a 'window' on the array, remove the first item each time and add a new one to the end. Also to open the file at the start of the function and close it at the end...
function array_smas_window($series, $length){ // Using for 'window'
$output = fopen(__DIR__."/file.txt", "w");
$window = array_slice($series, 0, $length);
$i = 0;
while(true) {
fwrite($output, print_r($window, true)."\n\n");
// If there are no more items, break the loop
if ( !isset($series[$length+$i]) ) {
break;
}
// Remove the first item
array_shift($window);
// Add a new one to the end
$window[] = $series[$length+$i++];
}
fclose($output);
}
This should also work for any window size as it carries on till there are no more items to add on.
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] =>
)
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, ",", '"', '')
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