background image

PHP 基础 PHP 读取 csv 文件内容的说明

以下主要对 PHP 读取 csv 文件的内容的分析介绍,供参考下
一次性读取 csv 文件内所有行的数据
代码如下:
<?php 

$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]."<br>";
}
} */

echo

 

$goods_list

[2][0];

fclose(

$file

);

?>
 
读取 csv 文件的某一行数据
代码如下:
 
<?php

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

;