+3 votes
in Programming Languages by (56.5k 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

+1 vote
by (348k points)
selected by
 
Best answer

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
)


...