gpt4 book ai didi

php - 将 .csv 读入 PHP 数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:53:21 24 4
gpt4 key购买 nike

<分区>

我有一个 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++;
}

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

如何将数据放入这个结构的数组中?

示例 .csv:

Row 1 will be field headings
Name, Gender, Ethnicity, DOB, etc .... the number of fields is not fixed - it could be from 30 to 40 fields in this row

Rows 2 onwards will be the records
John, M, British, 10/04/49, etc
Simon, M, British, 04/03/65, etc

ANSWER - 做了我想做的

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

$matrix = array();
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$matrix[] = $row;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com