日期:2014-05-17 浏览次数:20414 次
<?php 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 { foreach ($dataName as $k=>$v) { if($k == $c)//对应的字段 { $data[$v] = $dataSrc[$c]; } } } } if(!empty($data)) { $dataRtn[] = $data; unset($data); } $row++; } fclose($handle); return $dataRtn; } } function print_array($data) //错误在这个函数里 { $row = count($data); $column = count($data[0]); echo $row; echo $column; echo "<table>"; for($i=0;$i<$row;$i++) { echo "<tr>"; for($j=0;$j<$column;$j++) { echo "<td width=80 height=50>"; echo $data[$i][$j]; echo "</td>"; } echo "</tr>"; } echo "</table>"; } $aData = getCSVdata('test.csv'); //print_r($aData); print_array($aData); ?>