简体   繁体   中英

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:

保存为.csv

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

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>";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM