来自CSV的数据放在数组中(PHP数组)

时间:2021-06-27 13:39:34

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.

我目前在从CSV文件中读取数据时遇到问题。现在,当我打印数组时,它会显示CSV文件中的数据。

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

这是我用来从CSV文件中读取数据的代码

$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文件:

来自CSV的数据放在数组中(PHP数组)

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

有什么建议我可以像我想的那样使这项工作?提前致谢!

1 个解决方案

#1


3  

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

您在CSV中使用了不同的分隔符而非默认值,即:

fgetcsv

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

您可以在fgetcsv函数中将分隔符定义为第二个参数。

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

#1


3  

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

您在CSV中使用了不同的分隔符而非默认值,即:

fgetcsv

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

您可以在fgetcsv函数中将分隔符定义为第二个参数。

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