-
-
function getCSVdata($filename)
- {
- $row = 1;//第一行开始
- if(($handle = fopen($filename, "r")) !== false)
- {
- while(($dataSrc = fgetcsv($handle)) !== false)
- {
- $num = count($dataSrc);
- for ($c=0; $c {
- if($row === 1)//第一行作为字段
- {
- $dataName[] = $dataSrc[$c];//字段名称
- }
- else
- { // bbs.it-home.org
- foreach ($dataName as $k=>$v)
- {
- if($k == $c)//对应的字段
- {
- $data[$v] = $dataSrc[$c];
- }
- }
- }
- }
- if(!empty($data))
- {
- $dataRtn[] = $data;
- unset($data);
- }
- $row++;
- }
- fclose($handle);
- return $dataRtn;
- }
- }
-
- $aData = getCSVdata('test.csv');
- print_r($aData);
- ?>
复制代码
测试结果:
Array
(
[0] => Array
(
[a] => test3
=> test4
[c] =>
)
[1] => Array
(
[a] => test5
=>
[c] =>
)
)
>>> 更多有关php 导出csv php读写csv文件的内容。
|