Home > Backend Development > PHP Tutorial > PHP读取csv文件的内容

PHP读取csv文件的内容

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:30:54
Original
882 people have browsed it

一次性读取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);?>
Copy after login



读取csv文件的某一行数据

<?phpfunction 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);?>
Copy after login



读取csv文件制定行数(行区间)

<?phpfunction 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."<br>";}?>
Copy after login





Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template