Search code examples
phparrayscsvfgetcsv

Data from CSV placed behind each other in array (PHP array)


I'm currently having trouble with reading data from a CSV file. Now it shows the data from the CSV file like this when I print the array.

Array
(
    [0] => james;large;33
) 

However I want it to have it like this

Array
(
    [0] => james
    [1] => large
    [2] => 33
)

This is the code that I'm using to read data from the CSV file

$file = fopen($file, 'r');
    while ($row = fgetcsv($file) ) {
            print "<pre>";
            print_r($row);
            print "</pre>";
    }

And this is the CSV file that I'm using:

saved as .csv

Any suggestions how I can make this work like I want to? Thanks in advance!


Solution

  • You have different delimiter used in CSV rather default which is ,

    fgetcsv

    you can define delimiter in fgetcsv function as a second parameter.

    $file = fopen($file, 'r');
        while ($row = fgetcsv($file, 0, ";") ) {
                print "<pre>";
                print_r($row);
                print "</pre>";
        }