一次性讀取csv檔案內所有行的資料
複製代碼 代碼如下:
$file = fopen('windows_2011_s.csv','r');
while ($data = fgetcsv($file)) { //每次讀取CSV裡面的一行內容
//print_r($data); //此為一個數組,要獲得每一個資料,訪問數組下標即可
$goods_list[] = $data;
}
//print_r($goods_list);
/* foreach ($goods_list as $arr){
if ($arr[0]!=""){
echo $arr[0]."
";
}
} */
echo $goods_list[2][0];
fclose($file);
?>
讀取csv檔案的某一行資料
複製代碼 代碼如下:
function get_file_line( $file_name, $line ){
$n = 0;
$handle = fopen($file_name,'r');
if ($handle) {
while (!feof($handle)) {
++$n;
$out = fgets($handle, 4096);
if($line==$n) break;
}
fclose($handle);
}
if( $line==$n) return $out;
return false;
}
echo get_file_line("windows_2011_s.csv", 10);
?>
讀取csv檔案制定行數(行區間)
複製代碼 代碼如下:
function get_file_line( $file_name, $line_star, $line_end){
$n = 0;
$handle = fopen($file_name,"r");
if ($handle) {
while (!feof($handle)) {
++$n;
$out = fgets($handle, 4096);
if($line_star <= $n){
$ling[] = $out;
}
if ($line_end == $n) break;
}
fclose($handle);
}
if( $line_end==$n) return $ling;
return false;
}
$aa = get_file_line("windows_2011_s.csv", 11, 20); //從第11行到第20行
foreach ($aa as $bb){
echo $bb."
";
}
?>
另外從網上找的兩種方法(沒測試,不知道好不好使)
複製代碼 代碼如下:
$handle=fopen("1.csv","r");
while(!feof($handle)){
$buffer=fgetss($handle,2048);
$data=explode(",",$buffer);
$num=count($data);
for($i=0;$i<$num;$i++){
print_r($data);
}
}
?>
複製代碼 代碼如下:
$handle=fopen("1.csv","r");
$row=1;
while($data=fgetcsv($handle,1000,",")){
$num=count($data);
for($i=0;$i<$num;$i++){
echo $data[$i];
}
$row++;
}
?>
http://www.bkjia.com/PHPjc/327695.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327695.htmlTechArticle一次性讀取csv檔案內所有行的資料 複製代碼 代碼如下: ?php $file = fopen('windows_2011_s.csv','r'); while ($data = fgetcsv($file)) { //每次讀取CSV裡面的一行...