php str_getcsv array issue
15,188
str_getcsv() expects the string passed as parameter to be one record.
But since your source is a file anyway the easiest way is probably to use fgetcsv() instead of str_getcsv()
$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
$data[] = fgetcsv($fp);
}
fclose($fp);
self-contained example:
<?php
$targetFile = 'soTest.csv';
setup($targetFile);
$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
$data[] = fgetcsv($fp);
}
var_dump($data);
function setup($targetFile) {
file_put_contents($targetFile, <<< eot
1,name,address,town,state,zip,phone,website,other,other
2,name,address,town,state,zip,phone,website,other,other
3,name,address,town,state,zip,phone,website,other,other
eot
);
}
prints
array(3) {
[0]=>
array(10) {
[0]=>
string(1) "1"
[1]=>
string(4) "name"
[2]=>
string(7) "address"
[3]=>
string(4) "town"
[4]=>
string(5) "state"
[5]=>
string(3) "zip"
[6]=>
string(5) "phone"
[7]=>
string(7) "website"
[8]=>
string(5) "other"
[9]=>
string(5) "other"
}
[1]=>
array(10) {
[0]=>
string(1) "2"
[1]=>
string(4) "name"
[2]=>
string(7) "address"
[3]=>
string(4) "town"
[4]=>
string(5) "state"
[5]=>
string(3) "zip"
[6]=>
string(5) "phone"
[7]=>
string(7) "website"
[8]=>
string(5) "other"
[9]=>
string(5) "other"
}
[2]=>
array(10) {
[0]=>
string(1) "3"
[1]=>
string(4) "name"
[2]=>
string(7) "address"
[3]=>
string(4) "town"
[4]=>
string(5) "state"
[5]=>
string(3) "zip"
[6]=>
string(5) "phone"
[7]=>
string(7) "website"
[8]=>
string(5) "other"
[9]=>
string(5) "other"
}
}
edit2: For using the first element of each record as the key in the result array:
<?php
$targetFile = 'soTest.csv';
setup($targetFile);
$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
$row = fgetcsv($fp);
$id = array_shift($row);
$data[$id] = $row;
}
var_dump($data);
function setup($targetFile) {
file_put_contents($targetFile, <<< eot
1,name,address,town,state,zip,phone,website,other,other
2,name,address,town,state,zip,phone,website,other,other
3,name,address,town,state,zip,phone,website,other,other
eot
);
}
Author by
Drew Bartlett
Updated on July 30, 2022Comments
-
Drew Bartlett almost 2 years
I am uploading a csv file and then parsing it using str_getcsv. All works great except that I need a way to cycle through them. Ideally, it'd be great to have the array come back and look like this:
Array ( [1] => Array ( [0] => 1 // first id in csv [1] => name [2] => address [3] => town [4] => state [5] => zip [6] => phone [7] => website [8] => other [9] => other ) [22] => Array ( [10] => name [11] => address [12] => town [13] => state [14] => zip [15] => phone [16] => website [17] => other [18] => other ) [24] => Array ( [19] => name [20] => address [21] => town [22] => state [23] => zip [24] => phone [25] => website [26] => other [27] => other ) )
However the data comes back like the following:
Array ( [0] => 1 // first id in csv [1] => name [2] => address [3] => town [4] => state [5] => zip [6] => phone [7] => website [8] => other [9] => other 22 // id field [10] => name [11] => address [12] => town [13] => state [14] => zip [15] => phone [16] => website [17] => other [18] => other 24// id field [19] => name [20] => address [21] => town [22] => state [23] => zip [24] => phone [25] => website [26] => other [27] => other
Any suggestions on how to fix this to look like the array at the top? right now I'm doing:
$csvfile = file_get_contents($targetFile); $csv = str_getcsv($csvfile);
-
Drew Bartlett over 12 yearsthis returns the same thing still for me. I need it to look like the array on the top of the question
-
VolkerK over 12 years@Drew : Then maybe you have a problem with php not detecting the line-endings within the file. see docs.php.net/…
-
mseancole over 12 yearsI would say double check your csv and make sure each record isn't separated by a comma too. I haven't run this code but it looks good. and should result in the array you're looking for. oh, and each record should be on a newline too.
-
Solomon Closson over 7 yearsShould not use
\n
as determining a new row in the csv,\n
will break if any columns row has multiple lines in it.