Home Backend Development PHP Tutorial php怎么读取csv文件?

php怎么读取csv文件?

Jun 01, 2016 am 11:06 AM
csv file php Read data

php读取csv文件的方法:1、使用fopen()打开csv文件;2、使用fgetcsv()从文件指针中读入一行并解析 CSV字段;3、借助循环语句,将fgetcsv()读取的csv数据存入数组中;4、输出数组中的CSV数据即可。

php怎么读取csv文件?

php怎么读取csv文件?

1、文件内容

1.png

2、读取结果

2.png

3、代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public function testAction( ) {

    $filePath = APP_PATH.'/data/111.csv';

    $data = $this->getCsvData($filePath);

    var_export($data);

    die;

}

  

function getCsvData($filePath){

    $handle = fopen( $filePath, "rb" );

    $data = [];

    while (!feof($handle)) {

    $data[] = fgetcsv($handle);   

    }

    fclose($handle);

     

    $data = eval('return ' . iconv('gb2312', 'utf-8', var_export($data, true)) . ';');  //字符转码操作

     

    return $data;

}

Copy after login

说明:

fgetcsv — 从文件指针中读入一行并解析 CSV 字段

如果提供了无效的文件指针,fgetcsv() 会返回 NULL。 其他错误,包括碰到文件结束时返回 FALSE,。

这个函数比较关键,可以看到它的功能就是读取并解析 CSV 字段。这次没有用到太多的参数,有需要的看下方链接自行参考。

例1:一次性读取csv文件内所有行的数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?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

例2:读取csv文件的某一行数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?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;

  return false;

}

 

echo get_file_line("windows_2011_s.csv", 10);

?>

Copy after login

更多相关知识,请访问 PHP中文网!!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles