我有一个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
答案 - 做了我想要的事情
$file = $_FILES[csv1][tmp_name];
$handle = fopen($file,"r");
$matrix = array();
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$matrix[] = $row;
}
答案 0 :(得分:13)
你已经在PHP中实现了这种实现:
$csv = array_map('str_getcsv', file('someCSVfile.csv'));
说明:
str_getcsv()
- 将CSV字符串解析为数组。file()
- 将整个文件读入数组。array_map(Function, Array)
- 在将回调函数应用于每个元素后,返回包含数组所有元素的数组。示例输出:
//CSV
NAME,CLASS,ROOM
Sally,2018,312
Belinda,2017,148
//OUTPUT:
Array(
[0] => Array([0] => NAME, [1] => CLASS, [2] => ROOM),
[1] => Array([0] => Sally, [1] => 2018, [2] => 312),
[2] => Array([0] => Belinda, [1] => 2017, [2] => 148)
)
关联键
使用关联键解析两个快速函数 - 两者都基于array_map
方法:
方法1:运行两次
function assoc_getcsv($csv_path) {
$r = array_map('str_getcsv', file($csv_path));
foreach( $r as $k => $d ) { $r[$k] = array_combine($r[0], $r[$k]); }
return array_values(array_slice($r,1));
}
方法2:运行一次
function assoc_getcsv($csv_path) {
$f = array();
function parse_csv_assoc($str,&$f){
if (empty($f)) { $f = str_getcsv($str); }
return array_combine($f, str_getcsv($str));
}
return array_values(array_slice(array_map('parse_csv_assoc', file($csv_path), $f),1));
}
示例输出:
//CSV
NAME,CLASS,ROOM
Sally,2018,312
Belinda,2017,148
//OUTPUT:
Array(
[0] => Array([NAME] => Sally, [CLASS] => 2018, [ROOM] => 312),
[1] => Array([NAME] => Belinda, [CLASS] => 2017, [ROOM] => 148)
)
答案 1 :(得分:3)
此函数将读取CSV并生成多维数组
function fromCSVFile( $file) {
// open the CVS file
$handle = @fopen( $file, "r");
if ( !$handle ) {
throw new \Exception( "Couldn't open $file!" );
}
$result = [];
// read the first line
$first = strtolower( fgets( $handle, 4096 ) );
// get the keys
$keys = str_getcsv( $first );
// read until the end of file
while ( ($buffer = fgets( $handle, 4096 )) !== false ) {
// read the next entry
$array = str_getcsv ( $buffer );
if ( empty( $array ) ) continue;
$row = [];
$i=0;
// replace numeric indexes with keys for each entry
foreach ( $keys as $key ) {
$row[ $key ] = $array[ $i ];
$i++;
}
// add relational array to final result
$result[] = $row;
}
fclose( $handle );
return $result;
}
因此,如果您的.CSV文件如下所示:
Name,Gender,Ethnicity,DOB
John,M,British,10/04/49
Simon,M,British,04/03/65
你会得到这个:
(
[0] => Array
(
[name] => John
[gender] => M
[ethnicity] => British
[dob] => 10/04/49
)
[1] => Array
(
[name] => Simon
[gender] => M
[ethnicity] => British
[dob] => 04/03/65
)
)
答案 2 :(得分:3)
1.csv:
Name,Gender,Ethnicity,DOB
John,M,British,10/04/49
Simon,M,British,04/03/65
使用file()
将csv文件读入数组,然后遍历此数组并将所有字段转换为包含explode()
的数组元素并生成新的$array
<?php
$file = '1.csv';
$content = file($file);
$array = array();
for($i = 1; $i < count($content); $i++) {
$line = explode(',', $content[$i]);
for($j = 0; $j < count($line); $j++) {
$array[$i][$j + 1] = $line[$j];
}
}
print_r($array);
?>
输出:
Array
(
[1] => Array
(
[1] => John
[2] => M
[3] => British
[4] => 10/04/49
)
[2] => Array
(
[1] => Simon
[2] => M
[3] => British
[4] => 04/03/65
)
)
如果你想获得记录:2 和字段:4 ,你可以这样做:
echo $array[2][4];
输出:
04/03/65