+2 votes
in Programming Languages by (6.4k points)
I want to read a CSV file in such a way that each line becomes array elements. Is there any PHP function to a CSV file?

1 Answer

0 votes
by (26.1k points)

You can use the PHP function fgetcsv() that reads and outputs one line from the open CSV file.

Here is an example:

<?php
$file = fopen("abc.csv","r");
while(! feof($file)) {
    print_r(fgetcsv($file));
}
fclose($file);
?>

The file "abc.csv" has 2 lines and the output of the above code is as follows:

Array
(
    [0] => abc
    [1] => xyz
    [2] => 123 street
    [3] => DL
)
Array
(
    [0] => def
    [1] => tkr
    [2] => 456 street
    [3] => IN
)

...