相关疑难解决方法(0)

将.csv读入PHP数组

我有一个PHP例程,读取已上传到我的站点的.csv文件.

文件中的字段数可能与上传到上传不同.

我希望能够建立.csv文件的大小(字段数),然后将其内容存储在一个数组中.

这是我到目前为止:

//get the csv file 
$file = $_FILES[csv1][tmp_name]; 
$handle = fopen($file,"r"); 

//loop through the csv file and insert into array
$dataline = fgetcsv($handle,1000,",","'");
$fieldnum = count($dataline);

$recordloop = 0;

while ($dataline) 
{ 

    for ($fieldloop=0; $fieldloop <$fieldnum; $fieldloop++)
    {
        //this is the bit that is wrong
        $dataarray[]=array($dataline[$fieldloop]);
    }

    $data = fgetcsv($handle,1000,",","'");
    $recordloop++;
}
Run Code Online (Sandbox Code Playgroud)

例如,如果有30个字段和200个记录,我试图让数组成为结构$dataarray[200][30].

如何将数据导入此结构的数组中?

示例.csv:

Row 1 will be field headings
Name, Gender, Ethnicity, DOB, etc .... the number of fields is not fixed - …
Run Code Online (Sandbox Code Playgroud)

php csv arrays algorithm multidimensional-array

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

algorithm ×1

arrays ×1

csv ×1

multidimensional-array ×1

php ×1