Reputation: 472
I have an CSV file with multiple lines, and each line has more then 50 values all separated by '|' and i want to store each of those values into an array. The first line i get all values into the array with no problem, but the rest of the lines are passed into the array into a single key, for other words the rest of the lines dont break into those 50+ values. I've tried PHP read CSV file line by lines this already.
this is what i need:
Array
(
[0] => Array
(
[0] => SALE
[1] => FF
[2] => 17750478
[3] => 235
[4] => 2018-01-27T22:16:56
[5] => 2018-05-13T12:06:11
[6] =>
)
[1] => Array
(
[0] => SALE
[1] => FF
[2] => 10423478
[3] => 1006352300
[4] => 2018-01-27T22:16:56
[5] => 2018-05-13T12:06:11
[6] =>
)
[2] => Array
(
[0] => SALE
[1] => FF
[2] => 1204578
[3] => 1002362300
[4] => 2018-01-27T22:16:56
[5] => 2018-05-13T12:06:11
[6] =>
)
)
But i get :
Array
(
[0] => Array
(
[0] => SALE
[1] => FF
[2] => 17750478
[3] => 235
[4] => 2018-01-27T22:16:56
[5] => 2018-05-13T12:06:11
[6] =>
)
[1] => Array
(
[0] => SALE|FF|10423478|1006352300|2018-01-27T22:16:56|2018-05-13T12:06:11........
I've done a test and i took out alot of the values i left only lik 8 or 9 and the array finally comes how i want, but my file has 50+ values for each line. Is there a limit for breaking the values of each line? my code:
$OrderLines = array();
$file = fopen($InboundInno.$ACKFile, 'r');
while (($line = fgetcsv($file, false, '|' )) !== FALSE) {
$OrderLines[] = $line;
}
fclose($file);
EDIT I've notice that the last value of the first line(the only line that gets all values one by one) is getting a " in the last value. this loop add's a " on the last value of the first lane.
Upvotes: 0
Views: 666
Reputation: 472
Ok so i got it.
I dont know why, but this code adds a "
to the final of each line in my file like i said before. Ofcourse i did a test with the enclosure ($line = fgetcsv($file, 0, '|', ' " ')
and nothing.
Since i was desperate i inverted the enclosure and i did ($line = fgetcsv($file, 0, '|', " ' ")
and it works.
I don't know why PHP is treating the ' as a " but ok. Well it works now so hope this might help anyone in same situation. Here is full code:
while (($line = fgetcsv($file, 0, '|', "'")) !== FALSE) {
$OrderLines[] = $line;
}
fclose($file);
Upvotes: 1
Reputation: 87
I am using something like this to read CSV file:
function csv_decode($csv){
$output = array();
$lines = explode(PHP_EOL, $csv);
foreach($lines as $line){
$output[] = explode('|', $line);
}
}
Upvotes: 0