Reputation: 1037
I have a csv file that has the following struture :
"Trussville","L01","2352","Flatbed Truck"
"Trussville","L01","2336","Tote Truck"
"Trussville","L01","2379","Combo Truck"
"Trussville","L01","2238","Tankwagon (Glycol Truck)"
"Trussville","L01","2306","Combo Truck"
"Trussville","L01","2364","Tankwagon 2690 gal (650, 550, 400, 390, 350, 350)"
"Trussville","L01","2380","Combo Truck"
"Trussville","L01","2382","Tankwagon 3500 gal (900, 600, 400, 400, 500, 700)"
I'm using the following function to parse and read the csv data as following :
function fncRead($csvFile){
//Open the file in read mode
$file_handle = fopen($csvFile, 'r');
//Go through a loop
while (!feof($file_handle) ) {
// get data using fgetcsv and push to an array
$line_of_text[] = fgetcsv($file_handle, 1024);
}
//Close the file after reading
fclose($file_handle);
//return the array of data
return $line_of_text;
}
the following result I get is as following :
Array
(
[0] => Array
(
[0] => Trussville
[1] => L01
[2] => 2352
[3] => Flatbed Truck
"Trussville"
[4] => L01
[5] => 2336
[6] => Tote Truck
"Trussville"
[7] => L01
[8] => 2379
[9] => Combo Truck
"Trussville"
[10] => L01
[11] => 2238
[12] => Tankwagon (Glycol Truck)
"Trussville"
[13] => L01
[14] => 2306
[15] => Combo Truck
"Trussville"
[16] => L01
[17] => 2334
[18] => Tote Truck
"Trussville"
[19] => L01
[20] => 2337
[21] => 1000 gallon def compatment
"Trussville"
[22] => L01
[23] => 2338
[24] => DO NOT USE
"Trussville"
[25] => L01
[26] => 2364
[27] => Tankwagon 2690 gal (650, 550, 400, 390, 350, 350)
"Trussville"
..................
What I would like to have is this :
Array
(
[0] => Array
(
[0] => Trussville
[1] => L01
[2] => 2352
[3] => Flatbed Truck
),
[1] => Array
(
[0] => Trussville
[1] => L01
[2] => 2336
[3] => Tote Truck
),
.....
How can I do that please ?
Upvotes: 0
Views: 81
Reputation: 1717
It seems your file is missing line breaks (\r\n)
Try the following and see if you get the same results. If you do, then it is definitely the file. If it works, then perhaps there is something about the handling of the file using f-methods.
function fncRead($csvFile){
foreach(file($csvFile) as $value)
{
$$line_of_text[] = str_getcsv($value);
}
return $line_of_text;
}
Upvotes: 1
Reputation: 212402
Quoting from the PHP docs page for the fgetcsv() function:
Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.
Upvotes: 2