读的.csv成PHP数组 [英] Reading .csv into a PHP array
问题描述
我有读取已上传到我的网站.csv文件一个PHP程序。
文件中的字段的数目可以是从上载上传不同。
我希望能够确立.csv文件的大小(字段的数量),并存储其内容在数组中。
这是我到目前为止有:
//获取csv文件
$文件= $ _FILES [csv1] [tmp_name的值]
$处理= FOPEN($文件,R);
//通过CSV文件中环,并插入到数组
$数据线= fgetcsv($处理,1000,,');
$ fieldnum =计数($数据线);
$ recordloop = 0;
而($数据线)
{
为($ fieldloop = 0; $ fieldloop< $ fieldnum; $ fieldloop ++)
{
//这是位这是错误的
$ DataArray中[] =阵列($数据线[$ fieldloop]);
}
$数据= fgetcsv($处理,1000,,');
$ recordloop ++;
}
如果为例,有30个字段,200个记录,我试图让数组成为结构的 $ DataArray中[200] [30]
。
我如何获取数据到这个结构的阵列?
例.csv格式:
行1将字段标题
名,性别,种族,出生日期,等....字段的数目是不固定的 - 它可以是从该行中为30〜40的字段
2排起将记录
约翰,男,英国,49年10月4日,等
西蒙,男,英国,65年4月3日,等
答案 - 做正是我想要的。
$文件= $ _FILES [csv1] [tmp_name的值]
$处理= FOPEN($文件,R);
$矩阵=阵列();
而(($行= fgetcsv($处理,1000,))!= FALSE)
{
$矩阵[] = $行;
}
您已经有这种在PHP中实现:
$ CSV = array_map('str_getcsv',文件(someCSVfile.csv'));
说明:
-
str_getcsv()
- 解析一个CSV字符串转换成数组 -
文件()
- 整个文件读入一个数组 -
array_map(功能,阵列)
- 返回包含数组中的所有元素应用回调函数,每一个接一个数组 。
输出示例:
// CSV
姓名,班级,ROOM
莎莉,2018,312
贝琳达,2017,148
//输出:
阵列(
[0] =>阵列([0] =>的名称,[1] => CLASS,[2] =>房),
[1] =>阵列([0] =>萨利,[1] => 2018,[2] => 312),
[2] =>阵列([0] =>贝琳达,[1] => 2017,[2] => 148)
)
关联键
两个快速功能解析与联想键 - 都基于 array_map
方法:
方法1:运行两次
函数assoc_getcsv($ csv_path){
$ R = array_map('str_getcsv',文件($ csv_path));
的foreach($ R $为K => $ D){$ R [$ K] = array_combine($ R [0],$ R [$ K]); }
返回array_values(array_slice($ R,1));
}
方法2:运行一次
函数assoc_getcsv($ csv_path){
$ F =阵列();
功能parse_csv_assoc($海峡,与放大器; $ F){
如果(空($ F)){$ F = str_getcsv($海峡); }
返回array_combine($ F,str_getcsv($ STR));
}
返回array_values(array_slice(array_map('parse_csv_assoc',文件($ csv_path),$ f)中,1));
}
输出示例:
// CSV
姓名,班级,ROOM
莎莉,2018,312
贝琳达,2017,148
//输出:
阵列(
[0] =>阵列([NAME] =>莎莉,[CLASS => 2018年,[房间] => 312),
[1] =>阵列([NAME] =>贝琳达,[CLASS => 2017年,[房间] => 148)
)
I have a PHP routine that reads a .csv file that has been uploaded to my site.
The number of fields in the file may be different from upload to upload.
I want to be able to establish the size of the .csv file (the number of fields) and then store its contents in an array.
This is what I have so far:
//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++;
}
If for example, there are 30 fields and 200 records I am trying to get the array to be of the structure $dataarray[200][30]
.
How do I get the data into the array of this structure?
Example .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 - did just what I wanted
$file = $_FILES[csv1][tmp_name];
$handle = fopen($file,"r");
$matrix = array();
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$matrix[] = $row;
}
You already have this kind of implementation in PHP:
$csv = array_map('str_getcsv', file('someCSVfile.csv'));
Explanation:
str_getcsv()
- Parse a CSV string into an array.file()
- Reads entire file into an array.array_map(Function, Array)
- Returns an array containing all the elements of the array after applying the callback function to each one.
Example output:
//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)
)
Associative keys
Two quick functions to parse with associative keys - both based on the array_map
method:
METHOD 1: running twice
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));
}
METHOD 2: running once
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));
}
Example output:
//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)
)
这篇关于读的.csv成PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!