PHP读取CSV文件内容(示例)
时间:2021-07-01 10:21:17
帮助过:22人阅读
- function getCSVdata($filename)
- {
- $row = 1;//第一行开始
- if(($handle = fopen($filename, "r")) !== false)
- {
- while(($dataSrc = fgetcsv($handle)) !== false)
- {
- $num = count($dataSrc);
- for ($c=0; $c < $num; $c++)//列 column
- {
- 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文件的内容。 |